程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> ObjectC

ObjectC

編輯:關於C

// Created By 郭仔 2015年04月02日17:45:01

 

函數體外部分:

typedef int (^Block)(int ,int );//重命名

typedef void (^Block1)(int );

int n = 0;

=================================================================================================================

// 定義函數指針

int (*p)(int ,int) = sum;

//通過函數指針調用函數

int re = p(10,3);

printf("%d\n",re);

// int n = 4;

 

Block block = ^(int x,int y){

// NSLog(@"%d",n);

return x+y;

};

NSLog(@"%d",block(1,1));

 

//定義block

int (^block1)(int,int) = ^(int x,int y){

return x+y;

}; //最後分號別忘了

 

//調用block

int result = block1(10,34);

printf("%d\n",result);

 

//兩個練手例子

void (^block2)(int) = ^(int num){

printf("%d\n",num);

};

int (^block3)(int) = ^(int year){

return year;

};

block2(4);

printf("%d\n",block3(5));

 

int (^block4)(NSString *) = ^(NSString * str){

return [str intValue]; //對象方法intValue實現字符串轉化為數值

};

NSLog(@"%d",block4(@"333"));

//printf("%d\n",block4(@"333"));

 

 

/*

block是匿名函數:因為調用函數有兩種方式,一種是通過函數名調用,另一種是通過函數指針調用;

通過函數指針調用函數之前,需要給函數指針賦值,也需要用到函數名,所以對於匿名函數來說,這兩種方式都不能實現函數的調用,因此使用block;

block和函數指針非常相似,但是有本質上的區別,但在使用方面,區別不大,我們可以將block當成函數指針,這樣簡化blcok的使用;

*/

__block int x = 10;

// int y;

Block1 block5 = ^(int num){

 

x++;

// y++;//局部變量

n ++; //全局變量

NSLog(@"%d",x);

NSLog(@"%d",num);

};

 

block5(5);

 

 

/*

1.在block裡面能訪問block之外的局部變量,不能修改block之外的局部變量,如果需要修改,該局部變量前面加__block;

2.在block中可以修改全局變量的值;

3.__block聲明的變量會存儲在內存的全局區(靜態區)

*/

 

 

// =================================================================

 

Car * car1 = [Car carWithType:@"aa" andColor:@"bb" andPrice:134 andMaxSpeed:133 andCostOil:17];

 

Car * car2 = [Car carWithType:@"cc" andColor:@"dd" andPrice:234 andMaxSpeed:233 andCostOil:27];

 

Car * car3 = [Car carWithType:@"ee" andColor:@"ff" andPrice:334 andMaxSpeed:333 andCostOil:37];

 

Car * car4 = [Car carWithType:@"gg" andColor:@"hh" andPrice:434 andMaxSpeed:433 andCostOil:47];

 

NSMutableArray * cars = [NSMutableArray arrayWithObjects:car1,car2,car3,car4, nil];

 

//價格從高到低排序 block排序

//若用sortedArrayUsingComparator則需要重新定義一個數組保存(因為有返回值)

[cars sortUsingComparator:^NSComparisonResult(Car *obj1, Car *obj2) {

if ([obj1 price]<[obj2 price]) {

return NSOrderedDescending;

}

else if ([obj1 price] == [obj2 price])

return NSOrderedSame;

else

return NSOrderedAscending;

}];

 

for (Car *car in cars) {

NSLog(@"%@",car);

}

 

// maxSpeed從大到小排序,使用sortedUsingComparator

NSArray * arr = [cars sortedArrayUsingComparator:^NSComparisonResult(Car *obj1, Car *obj2) {

if ([obj1 maxSpeed]<[obj2 maxSpeed]) {

return NSOrderedDescending;

}

else if ([obj1 maxSpeed]==[obj2 maxSpeed])

return NSOrderedSame;

else

return NSOrderedAscending;

}];

 

for (Car * car in arr) {

NSLog(@"%@",car);

}

 

 

//自己定義block

NSComparisonResult (^block6)(Car *,Car*) = ^(Car *obj1,Car *obj2){

if ([obj1 maxSpeed]<[obj2 maxSpeed]) {

return NSOrderedDescending;

}

else if ([obj1 maxSpeed]==[obj2 maxSpeed])

return NSOrderedSame;

else

return NSOrderedAscending;

 

};

//然後調用sort方法,block6傳參過去

[cars sortUsingComparator:block6];

 

for (Car * car in cars) {

NSLog(@"%@",car);

}

 

// ==============================================================

//字面量

//數組字面量

NSString * str = @"Literals";

//數組字面量

NSArray * arr1 = @[@"1",@"2",@"3"];

for (NSString * s in arr1) {

NSLog(@"%@",s);

}

//字典字面量

NSDictionary * dic = @{@"name":@"zhangsan",@"age":@"6"};

NSLog(@"%@,%@",dic[@"name"],dic[@"age"]);

 

 

NSNumber * num = @123;//把數字123轉化成NSNumber對象

NSLog(@"%@",num);

// ===============================================================



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved