OC的一些基本声明方法整理

作者: f1766bdadc56 | 来源:发表于2016-06-19 20:40 被阅读0次

(本文参考资料整理,仅作为初学笔记)

一,字符串

1.字符串字面量

NSString *a=@"this is a";

2.字符串对象初始化

NSString *aString = [[NSString alloc] initWithString:@"some string literal"];//(分配内存并初始化方法)

NSString *aString = [NSString stringWithString:@"some string literal"];(便捷方法)

3.格式化字符串

int value=3;

NSString *aString = [NSString stringWithFormat:@ "value is %d",value];

(补充:int(32位)-%d,unsigned int-%u,long(32位或64位)-%ld,unsigned long-%lu,float-%f,NSObject子类实例-%@)

4.对象格式说明符

NSString *aString=@"criminals";

NSString *anotherString = [NSString stringWithFormat :@"When you %@ strings,only %@ will use strings. ",@"criminalize",aString];

5.字符串比较

NSString *a=@"hello";

NSString *b=@"hello";

BOOL stringsAreEqual=([a isEqualToString:b]);

二,数字

NSNumber的使用

NSNumber *aNumber = [NSNumber numberWithInt:3];

NSLog(@"I have %@ apples",aNumber);

NSLog(@"I have %d apples",[aNumber intValue]);

三,数组

1.创建NSArray对象示例

NSArray *emptyArray = [NSArray array];

NSArray *firstList=[NSArray arrayWithObjects:@"apple","bananas","milk",nil];

NSArray *secList=[[NSArray alloc] initWithObjects:@"apples",@"bananas",@"milk",nil];

NSArray *thirdList=[NSArray arrayWithArray:secList];

2.数组字面语法

NSArray *array = @[@"hello",NSApp,[NSNumber numberWithInt:3]];

3.异构数组

NSArray *array=[NSArray arrayWithObjects:@"a string",[NSNumber numberWithBool:YES],nil];

4.数组下标

NSString *str=myArray[3];

四,集合

1.集合的声明

NSSet *set=[NSSet setWithObjects:@"apples",@"milk",@"bananas"];

2.测试对象是否包含在集合中

NSSet *set=[NSSet setWithObjects:@"apples",@"milk",@"bananas"];//集合的声明

NSString *string=@"apples";

if([set containsObject:string])//测试对象是否包含在集合中

NSLog(@"YES!");

NSArray *array=[set allObjects];//赋值给数组

for(NSString *string in array){//遍历数组

NSLog(@"%@",string);

}

3.可变集合

NSMutableSet *mutableSet=[NSMutableSet setWithObjects:@"apple",@"bananas",@"milk",nil];//声明可变数组

[mutableSet intersectSet:[NSSet setWithObject:@"apple"]];//现在只有"apples"了

[mutableSet uniontSet:[NSSet setWithObject:@"apple",@"bread",@"jam",nil]];//现在有"apple","bread","jam"

[mutableSet minusSet:[NSSet setWithObject:@"bread"]];//现在有"apple","jam"

[mutableSet minusSet:[NSSet setWithObject:@"pasta"]];//没有变化

五,字典

1.使用字典

NSArray *keys=[NSArray arrayWithObjects:@"name",@"idNum",nil];

NSArray *values=[NSArray arrayWithObjects:@"Bob",[NSNumber numberWithInt:123],nil];

NSMutableSet *dic=[NSDictionary dictionaryWithObjects:values forKeys:keys];

NSLog(@"dic count :%d",[dic count]);//得到2

NSLog(@"name is:%@",[dic objectForKey:@"name"]);//得到Bob

NSLog(@"birth is:%@",[dic objectForKey:@"birth"];//得到nil

2.遍历字典

NSArray *keys=[NSArray arrayWithObjects:@"name",@"idNum",nil];

NSArray *values=[NSArray arrayWithObjects:@"Bob",[NSNumber numberWithInt:123],nil];

NSMutableSet *dic=[NSDictionary dictionaryWithObjects:values forKeys:keys];

for(NSString *key in[dic allKeys]){

NSLog(@"%@ => %@",key,[dic objectForKey:key]);

}

3.字典字面量

NSDictionary *dic=@{

@"name":NSUserName(),

@"data":[NSDate date],

@"processInfo":[NSProcessInfo processInfo]

};

4.字典下标语法

id key=......;

id value=object[key];

相关文章

  • OC的一些基本声明方法整理

    (本文参考资料整理,仅作为初学笔记) 一,字符串 1.字符串字面量 NSString *a=@"this is a...

  • swift自学笔记(一)基础部分

    下面我写的都是在OC的基础上整理出来的一些笔记: 1.swift一些基本的属性 (1)变量和常量的声明:在swif...

  • 2018-07-06 有关于协议

    OC中的协议中用于声明一些方法,供其他类去遵守或实现,通常用于代理模式。 1、协议要写在.h文件中,只能声明方法,...

  • OC&iOS

    OC语言基础 1.类与对象 类方法 OC的类方法只有2种:静态方法和实例方法两种 在OC中,只要方法声明在@int...

  • Objective-C 类与对象

    类方法 OC 中类的方法只有类的静态方法和类的实例方法 OC 中的方法只要声明在 @interface 里,就可以...

  • OC转Swift

    记录下OC转Swift的一些经历 文件区别 OC:.h声明 .m实现 Swift:.swift 声明+实现 参数...

  • iOS中的Protocol

    Protocol基本概念 类似于java的接口interface,接口就是一堆方法的声明,没有实现,在OC中接口是...

  • 私有变量(上)

    // 如果只有方法的实现, 没有方法的声明, 那么该方法就是私有方法 // 在OC中没有真正的私有方法, 因为OC...

  • OC和Swift混编遇到的一些小麻烦

    一. OC中调用swift类中的方法时,编译器有时找不到方法声明 OC调用swift类中的方法,swift类需要有...

  • 2018-04-01

    oc中对于类的属性声明和实现其getter和setter方法,oc 2.0中增加了属性声明,就是可以不用单独为每一...

网友评论

    本文标题:OC的一些基本声明方法整理

    本文链接:https://www.haomeiwen.com/subject/zspbdttx.html