美文网首页与时俱进的IT
OC琐碎点汇总(一)

OC琐碎点汇总(一)

作者: 社会主义顶梁鹿 | 来源:发表于2018-07-29 16:46 被阅读0次

 

- (void)方法名:(参数类型)参数名 方法名:(参数类型)参数名

{

 具体实现代码

}

*方法的调用

[对象名 方法名];

#pragma -----字符串----

**//处理字符串

//1.把字符串转成大写(字母)up转大写

uppercaseString

//2.把字符串转成小写  low转小写

lowercaseString

//3.只让首字母大写 其他小写

capitalizedString

**//判断字符串是不是以什么字符开头

BOOL 布尔类型 只有YES NO两种结果 它的默认值是NO

BOOL a = [name hasPrefix:@"130"] ;

**//判断字符串是不是以什么结束

hasSuffix

// **** NSRange:范围 (包括起点位置和长度)

NSRange range;

range.location = 1; 起点位置

range.length = 3; 长度

%lu 无符号长整形 长度是一个数 没有符号

%d 有符号长整形

判断某字符串在不在这个范围

rangeOfString

Range.location!=NSNotFound 没有找到

**//判断两个字符串是否相同

isEqualToString

**//截取字符串

//从开始截取到哪里

substringToIndex

//从哪里截取到最后

substringFromIndex

//从哪一个位置截取到哪一个位置

substringWithRange:range

**//字符串拼接

//1.在string这个字符串的基础之上,拼接一段字符串 ->生成一个新的字符串 string无变化

stringByAppendingString

//2.格式化拼接字符串方法

stringByAppendingFormat

//3.使用格式化字符串 ->把其他格式的数据转化成字符串

stringWithFormat:

float score = 90.5;

NSString *s = [NSString stringWithFormat:@"%0.1f",score];

**//数据类型转换

整形 intValue

float型 floatValue

#pragma ---可变字符串NSMutableString-----

**//增、拼接字符串

[NSMutableString stringWithString:@"直接格式化字符串的方法"] ;

appendString:添加字符串

appendFormat:其他格式的都可以添加

int a = 250;

[string appendFormat:@"独步闯天下%d",a];

**//插入字符串 可以随意插入到指定位置

insertString:@"内容" atIndex:位置(6)

**//删除字符串

deleteCharactersInRange:range

**//改、替换

replaceCharactersInRange:rangewithString:@""

#pragma ----------数组--------

**//初始化数组

1、初始化方式1--直接赋值 @[元素,元素]

2、初始化方式2

NSArray *list = [[NSArray alloc]init];

3、初始化方式3

NSArray *list = [NSArray array];

**//数组取值

数组取值:下标(索引)取值,下标是从0开始的

**//数组遍历

数组遍历.1

获得数组元素个数的属性: count

使用方法:数组名.count

然后用for循环遍历出来

数组遍历.2 快速遍历                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

for (元素类名 *对象名(所取元素) in 数组名) {

NSLog(@"%@",所取元素);

}

**//字符串里面可以通过符号截取字符串变成数组

componentsSeparatedByString:@"字符串中的特定字符"

**//数组变成字符串

1.componentsJoinedByString的意思:加入一个字符串使之返回一个字符串

2.把数组格式化字符串

NSString *s = [NSString stringWithFormat:@"%@",list];

#pragma ---可变数组 NSMutableArray-------

注意!:不可以直接给可变数组赋值*******

**//可变数组的增

[list addObject:hero];

**//删除元素

arrayWithObjects:某个元素,某个元素,nil

可以通过下标来删除

(1)[list removeObjectAtIndex:1];

[list removeObjectsInRange:range];

移除最后一个元素

[list removeLastObject];

移除全部内容

[list removeAllObjects];

**//替换

[list replaceObjectAtIndex:1withObject:@"hanyang"];

**//查找

//1.通过下标,查找元素所在位置

list[0];

//2.通过元素,查找元素所在位置

NSString *name = @"tom";

NSUInteger i = [list indexOfObject:name];

NSLog(@"%lu",(unsigned long)i);

#pragma --------字典-------

key是一个关键字(字符串)

**//初始化字典并赋值

//(1)直接赋值  字典直接赋值用花括号@{};

//key 不能一样

NSDictionary *dic = @{@"key":@"值",@"key1":@"值"};

//字典嵌套字典                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

NSDictionary *dic = @{@"age":@"20",@"Like":@{@"Sports":@"PingPang",@"Art":@"SuMiao",@"Music":@"Sing"}};

//打印出来的结果用花括号引起来,每个元素后面都有一个分号;都是成对出现。

**//字典取值

公式:字典名【@“key名”】

**//遍历字典

//需要所有的key

//allkey 可以获得字典里面所有的key

//allValues 可以获得字典里面所有的值

NSArray *allkey = size2.allKeys;

 for (NSString *key in allkey) {

 NSLog(@"%@ = %@",key,size2[key]);

        }

**//其他初始化方式

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"值",@"key",@"值2",@"key",nil];

**//字典取值的方法 objectForKey

NSString *value = [dicobjectForKey:@"key"];

NSDictionary *baby = [NSDictionarydictionaryWithObjectsAndKeys:@"key",@"key",@"key",nil];

**//初始化,并且附一个值

NSDictionary *dic = [NSDictionary dictionaryWithObjects:@"值"forKeys:@"key"];

#pragma ------可变字典--------

NSMutableDictionary

**//可变字典 增

[dic setObject:@"内容:背着书包去上学" forKey:@"content"];

**//可变字典 删

//删除一个

[dic removeObjectForKey:@"key"];

//删除多个

[dic removeObjectsForKeys:@"key1",@"key2"];

//会移除数组里面所有key对应的值

[a removeObjectsForKeys:keys];

**//可变字典 改

//修改和添加一样 如有有相同的key就是覆盖 没有的话就是添加

[a setObject:@"一个雨夜" forKey:@"相同的key"];

#pragma ------属性----------

setter:公式

 - (void)set属性名:(属性类型)形参名;

 getter:公式

 - (属性类型)属性名;

**//属性的定义公式

 在.h 文件里,@property 属性类型 属性名;

**//定义构造方法:

 1.公式:

 - (id)initWith要初始的名字:(数据类型)形参名

 {

 self = [super init];

 if(self){

 }

 return self;

 }

#pragma ------封装、继承、多态--------

/*面向对象语言的三大特征

 1.封装:把所有共性东西放到一起

   *好处:(1)代码清晰(2)模块化,方便使用,减少代码量(3)复用性高,便于维护

 2.继承:子类、父类(超级类)

  *子类父类的关系:

 (1)父类有的方法,子类可以使用

 (2)父类有的属性,子类可以使用

 (3)父类所有公开的方法属性,子类都可以使用

 (4)子类独有的方法属性,父类不能使用

 3.多态:同一个方法有不同的实现,就叫做多态。

 oc->多继承? 可以使用类目(category)

 没有多继承04 

 只能单继承,多级继承(一个一个继承)

相关文章

网友评论

    本文标题:OC琐碎点汇总(一)

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