5.25 类的引用与传值
来源:互联网 发布:阿里云如何安装xampp 编辑:程序博客网 时间:2024/06/08 09:38
1,类的引用
2,交叉引用与循环引用
3,传值的方式
4,数据持久化
5,NSUserDefaults的使用
6,单例
// Observer.h#import <Foundation/Foundation.h>#import "Person.h"#import "ClassRoom.h"@interface Observer : NSObject@property (strong, nonatomic) Person *per;@property (strong, nonatomic) ClassRoom *classRoom;@property (strong, nonatomic) NSString *keyword;@end
// Observer.m//----属性传值//1.A.xx -> B.xx//2.A.xx -> C.xx// C.xx -> B.xx//----初始化/代理 方法传值//----数据持久化(将任意数据归档之后,存放进本地文件)//关联式引用#import "Observer.h"#import "Computer.h"@implementation Observer- (instancetype)init{ self = [super init]; if (self) { NSLog(@"observer init"); self.per = [[Person alloc]init]; self.per.keyword = self.keyword;// _per.computer.keyword = _per.keyword; _classRoom = [[ClassRoom alloc]init]; _classRoom.keyword = _per.keyword; [self actionBegin]; } return self;}- (void)actionBegin{ [_per walkToClassRoom:_classRoom];// [_per walkToClassRoom:_classRoom]; NSLog(@"persons = %@",_classRoom.persons);}
// ClassRoom.h#import <Foundation/Foundation.h>@interface ClassRoom : NSObject//给ClassRoom设置一个strong mutable的Array属性@property (strong, nonatomic) NSMutableArray *persons;//先初始化后使用@property (strong, nonatomic) NSString *keyword;@end
// ClassRoom.m#import "ClassRoom.h"@implementation ClassRoom- (instancetype)init{ self = [super init]; if (self) { _persons = [NSMutableArray array]; } return self;}@end
// Person.h#import <Foundation/Foundation.h>@class Computer;@class ClassRoom;@interface Person : NSObject//{// Computer *_computer;//}@property (strong, nonatomic) NSString *name;@property (strong, nonatomic) Computer *computer;//strong: 在使用属性的过程中,保证对象存在//retain:@property (strong, nonatomic) NSString *keyword;//建立一个便利构造,通过电脑对象初始化Person+ (instancetype)personWithComputer:(Computer *)computer;- (void)walkToClassRoom:(ClassRoom *)classRoom;@end
// Person.m//// -------- computer属性的存在时间//------------ person的存在时间#import "Person.h"#import "Computer.h"#import "ClassRoom.h"@implementation Person- (instancetype)init{ self = [super init]; if (self) { NSLog(@"person init"); _computer = [[Computer alloc]init];// _computer.keyword = self.delegate.keyword; _computer.delegate = self; } return self;}+ (instancetype)personWithComputer:(Computer *)computer{ Person *per = [[self alloc]init]; per.computer = computer; per.computer.delegate = per;// per.point.x = 5;//// [per autorelease];//在方法中加入autorelease,使其内存管制无误,有且仅有一个 return [per autorelease];}- (void)walkToClassRoom:(ClassRoom *)classRoom{ [classRoom.persons addObject:self];}- (void)dealloc{ [_computer release]; [super dealloc];}@end
// Computer.h#import <Foundation/Foundation.h>#import "Person.h"@interface Computer : NSObject@property (assign, nonatomic)Person *delegate;//上一级//assign: 弱引用,不增加引用计数,保证对象即时释放//weak:@property (strong, nonatomic) NSString *keyword;@end
// Computer.m#import "Computer.h"@implementation Computer- (instancetype)init{ self = [super init]; if (self) { NSLog(@"computer init"); } return self;}//循环引用- (void)dealloc{ self.delegate = nil;//研究知识点:当owner是strong属性和assign属性的时候,分别有什么作用 [super dealloc];}@end
// main.m#import <Foundation/Foundation.h>#import "Observer.h"#import "Person.h"#import "Computer.h"int main(int argc, const char * argv[]) { @autoreleasepool {/* Person *per = [[Person alloc]init];// Computer *computer1 = [[Computer alloc]init];// per.computer = [computer1 autorelease];// [per release]; per.name = [NSString stringWithFormat:@"abc"];//retaincount = 2 per.name = [NSString stringWithFormat:@"bcd"];//2 per.name = nil;//没有 //对象的内存什么时候释放? */ Observer *ob = [[Observer alloc]init]; NSArray *array = @[@"keyword"]; [array writeToFile:@"/Users/wangzheng/Desktop/classSeven.txt" atomically:YES]; NSArray *array1 = [NSArray arrayWithContentsOfFile:@"/Users/wangzheng/Desktop/classSeven.txt"]; NSLog(@"%@",array1); //NSUserDefaults [[NSUserDefaults standardUserDefaults]setObject:@"12345" forKey:@"account"];//将数据存入本地路径(移动设备路径) BOOL succeed = [[NSUserDefaults standardUserDefaults]synchronize]; //获取到Document和Library NSString *account = [[NSUserDefaults standardUserDefaults]objectForKey:@"account"]; NSLog(@"%@",account); //数据持久化 //对象归档 NSKeyedArchiver NSKeyedUnarchiver //网络请求 服务器 // 云 icloud NSLog(@"Hello, World!"); } return 0;}
0 0
- 5.25 类的引用与传值
- 举例说明:传值引用与传地址引用的区别
- 传值与传引用的区别
- 传值与传引用的 区别
- Java的传值与传引用
- 变量的传值与传引用
- JAVA 所谓的传值 与引用
- python的传值与引用
- c++的传值与引用
- 类对象的“传值”与“传引用”
- 引用与数组,引用与类,引用与返回值,引用与多态,const引用,引用类型
- 传值与引用
- php 引用传值与引用返回
- 传引用与传值
- 传值与传引用
- 传值与传引用
- OC 引用与传值
- 关于JAVA的传值与引用的理解
- Android高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- linux shell 编程常需要的注意点 (一)
- OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)的用法
- 洞悉linux下的Netfilter&iptables:内核中的ip_tables分析
- tomcat异常:org.apache.tomcat.util.http.Parameters processParameters
- 5.25 类的引用与传值
- 数据库日期范围查询
- Github上600多个iOS开源项目分类及介绍
- java 单例模式
- mysql错误:Can’t create TCP/IP socket (10106)
- iOS--对iOS时间类的代码总结 NSCalendar NSDateComponents NSDate
- 利用oracle存储过程发送邮件附档
- MAC如何打开远程linux服务器的图形化软件
- maven中去掉单元测试的配置