iOS学习——第三天

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));//查看strretainCount,为2;如果是weakstrretainCount则不一定

    __weak NSMutableString *weakstr2=str;

    NSLog(@"weakstr2 %ld",CFGetRetainCount((__bridgeCFTypeRef) weakstr2));

    __weak NSMutableString *weakstr3=weakstr;

   //指向weak referenceweak 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.查看NSMutableStringretainCount

   NSMutableString *s1=[NSMutableStringstringWithFormat:@"%@",@"One"];

   NSMutableString *s2=[NSMutableStringstringWithFormat:@"%@",@"Two"];

   NSMutableString *s3=[NSMutableStringstringWithFormat:@"%@",@"Three"];

   NSArray *array=[[NSArrayalloc]initWithObjects:s1,s2,s3,nil];//字符串的所有权归array,s1,s2,s3retainCount1

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]一个个取出的keyid可换为任意类型,此处可换为NSString

    {

        id value=[dic objectForKey:keyInDictionary];

        NSLog(@"key = %@,objectForKey = %@",keyInDictionary,value);

    }


//其它还有多种方法




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值