1.数组的排序
1
2
3
4
5
6
7
8
|
-(
void
) sort
{
//[book sortUsingSelector:@selector(compareNames:)];
[book sortUsingComparator:
^(id obj1, id obj2){
return
[[obj1 email] compare: [obj2 email]];
}];
}
|
2.使用copy协议的时候,需要在代码实现类中,为新方法添加下列定义
1
2
3
4
5
6
|
-(id) copyWithZone: (NSZone *) zone
{
Fraction *newFract = [[Fraction allocWithZone: zone] init];
//Fraction是类名
//[newFract setTo: number1 over: number2]; 这里可以写一些copy的时候需要执行的语句,也可以不写
return
newFract;
}
|
3.NSString与int和float的相互转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
NSString *tempA = @
"123"
;
NSString *tempB = @
"456"
;
//1,字符串拼接
NSString *newString = [NSString stringWithFormat:@
"%@%@"
,tempA,tempB];
//2,字符转int
int
intString = [newString intValue];
//3,int转字符
NSString *stringInt = [NSString stringWithFormat:@
"%d"
,intString];
//4,字符转float
float
floatString = [newString floatValue];
//5,float转字符
NSString *stringFloat = [NSString stringWithFormat:@
"%f"
,intString];
|
4.C中的char类型指针变量比较,需要用strncmp
1
2
3
4
5
6
7
8
9
|
/**
函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
说明:此函数功能即比较字符串str1和str2的前maxlen个字符。如果前maxlen字节完全相等,返回值就=0;在前maxlen字节比较过程中,如果出现str1[n]与str2[n]不等,则返回(str1[n]-str2[n])。
*/
if
(!strncmp(fuhao,
"c"
,
1
)) {
NSLog(@
"dd"
);
}
|
5.分段按钮键值的获取方法
1
2
3
|
//colorIndex为视图中分段的实例名
int
colorNum;
colorNum = colorIndex.selectedSegmentIndex;
|
6.分段按钮标题的获取方法
1
2
|
NSString *navStr;
navStr = [numNav titleForSegmentAtIndex: numNav.selectedSegmentIndex];
|
7.加载远程web页面
1
2
3
4
|
//webView为视图中,UIView的实例名
NSURL *appUrl;
[webView loadRequest:[NSURLRequest requestWithURL:appUrl]];
|