1.assign+copy
//Parent.h
@interface Parent :NSObject
@property (assign,nonatomic)NSMutableString *a;
@property (copy,nonatomic)NSMutableString *b;
@end
//AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSMutableString *str=[NSMutableStringstringWithFormat:@"%@",@"test"];
p.a=str;
p.b=p.a;
return YES;
}
copy:复制一份新的
assign:指针指向同一地址
2.weak+strong
本地变量前面要加两个下划线,成员变量不用加
NSMutableString *str=[NSMutableStringstringWithFormat:@"%@",@"test"];//“=”后的部分创建一个临时变量,引用计数加1;再赋值,引用计数又加1
NSLog(@"str %ld",CFGetRetainCount((__bridgeCFTypeRef) str));
__weak NSMutableString *weakstr=str;
NSLog(@"str %ld",CFGetRetainCount((__bridgeCFTypeRef) str));//查看str的retainCount,为2;如果是weakstr的retainCount则不一定
__weak NSMutableString *weakstr2=str;
NSLog(@"weakstr2 %ld",CFGetRetainCount((__bridgeCFTypeRef) weakstr2));
__weak NSMutableString *weakstr3=weakstr;
//指向weak reference的weak reference,会对引用计数加1
NSLog(@"str %ld",CFGetRetainCount((__bridgeCFTypeRef) str));
__strongNSMutableString *strongstr=str;//每用一次strong则相当于对str retain一次
NSLog(@"str %ld",CFGetRetainCount((__bridgeCFTypeRef) str));
3.NSArray
1.构造多个NSMutableString
2.构造一个NSArray
3.把NSMutableString对象添加到NSArray
4.查看NSMutableString的retainCount
NSMutableString *s1=[NSMutableStringstringWithFormat:@"%@",@"One"];
NSMutableString *s2=[NSMutableStringstringWithFormat:@"%@",@"Two"];
NSMutableString *s3=[NSMutableStringstringWithFormat:@"%@",@"Three"];
NSArray *array=[[NSArrayalloc]initWithObjects:s1,s2,s3,nil];//字符串的所有权归array,s1,s2,s3的retainCount加1
NSLog(@"%d",[s1retainCount]);
[s1release];[s2release];[s3release];//这个时候字符串release,则在array销毁后字符串的retainCount也变为0
NSLog(@"%d",[s1retainCount]);
OUTPUT:2 1
把NSArray改为NSMutableArray:
5.NSArray移除相应的NSMutableString
6.查看相应的NSMutableString的retainCount
NSMutableArray *array=[[NSMutableArray alloc] initWithObjects:s1,s2,s3, nil];
NSLog(@"%d",[s1retainCount]);
[array removeObject:s1];
NSLog(@"%d",[s1retainCount]);
OUTPUT:2 1
可以用索引输出:NSLog(@"%@",array[0]);
4.NSNumber
用NSNumber类来用面向对象的方法处理数字。如果你只需要简单的数字(而不是对象),用NSInterger类来操作有符号数(正数或者负数),用NSUInterger类来操作无符号数(正数或0),用CGFloat类和double来操作浮点数。
numberWithInt:将一个整数值封装成一个NSNumber实例;
numberWithUnsignedInt:将一个无符号整数值(正数或0)封装成一个NSNumber实例;
numberWithFloat:将一个浮点数封装成一个NSNumber实例;
numberWithDouble:将一个double类型的数封装成一个NSNumber实例;
intValue:从调用该函数的NSNumber实例中返回一个整型NSIteger类型值。
unsignedIntValue:从调用该函数的NSNumber实例中返回一个无符号整型NSIteger类型值。
floatValue:从调用该函数的NSNumber实例中返回一个浮点数CGFloat类型值。
doubleValue:从调用该函数的NSNumber实例中返回一个双精度double类型值。
5.NSDictionary
NSArray *a1=[[NSArrayalloc]initWithObjects:@"1",@"2",@"3",nil];//objectForKey
NSArray *a2=[[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil];//keys
NSDictionary *dic=[NSDictionarydictionaryWithObjects:a1forKeys:a2];
for (id keyInDictionaryin [dic allKeys])//[dic allKeys]把所有的key取出来,利用in keyInDictionary里存从[dic allKeys]一个个取出的key,id可换为任意类型,此处可换为NSString
{
id value=[dic objectForKey:keyInDictionary];
NSLog(@"key = %@,objectForKey = %@",keyInDictionary,value);
}