iOS前期OC训练OC_10Block
//
//? main.m
//? OC10_Block
//
//? Created by dllo on 15/7/28.
//? Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
// C語言的函數
//NSInteger maxValue(NSInteger a, NSInteger b)
//{
//? ? return a > b ? a : b ;
//}
void test1()
{
? ?NSLog(@"今天好熱");
}
int addNum(int a,int b){
? ?return a + b;
}
typedef int DT;
// 新的類型寫在*的后面
typedef int (*PFUN)(int,int);
typedefNSString *(^Block)(NSArray *);
// 有返回值,無參數
typedefNSInteger (^Integer)();
int main(int argc,const char * argv[]) {
? ?
//? ? // 棧
//? ? int a = 10;
//? ? NSLog(@"%p", &a);
//? ? // 堆
//? ? int *p = malloc(40);
//? ? NSLog(@"%p", p);
//? ? // 全局靜態區
//? ? static int b = 10;
//? ? NSLog(@"%p", &b);
//? ? // 常量區
//? ? char *str = "iPhone";
//? ? NSLog(@"%p", str);
//? ? // 代碼區
//? ? NSLog(@"%p", maxValue);
// ? ?
//? ? // 函數指針的類型
//? ? NSInteger (*pointer)(NSInteger, NSInteger) = maxValue;
//? ? NSLog(@"%p", pointer);
//? ? NSLog(@"%ld", maxValue(10, 5));
//? ? // 函數指針的調用
//? ? NSLog(@"%ld", pointer(10, 5));
?? ?
? ? Block的四種形式
? ? /// 1.無參數,無返回值
//? ? void (^block)() = ^(){
//? ? ? ? NSLog(@"今天有點云");
//? ? };
// ? ?
//? ? // block的使用
//? ? block();
? ? // block自己不會執行,必須調用才會執行block大括號里的代碼段
? ? /// 2.有參數,無返回值
//? ? void (^block)(int, int) = ^(int a, int b){
//? ? ? ? int max = a > b ? a : b;
//? ? ? ? NSLog(@"%d", max);
//? ? };
// ? ?
//? ? // 調用
//? ? block(10, 20);
? ? // void (^)(int, int) 是block的類型,它的名是block,我們也通過名進行調用
?? ?
//? ? void (^block)(NSString *) = ^(NSString *str){
//? ? ? ? NSLog(@"%@", str);
//? ? };
//? ? block(@"nihao");
?? ?
? ? //參數是一個數組,對數組進行遍歷
//? ? void (^block)(NSArray *) = ^(NSArray *arr){
// ? ? ? // NSLog(@"%@", arr);
//? ? ? ? for (NSString *str in arr) {
//? ? ? ? ? ? NSLog(@"%@", str);
//? ? ? ? }
//? ? };
//? ? block(@[@"2", @"3", @"5", @"4"]);
?? ?
? ? /// 3.有返回值無參數
//? ? // 有返回值在函數的最后要加上return
//? ? int (^block)() = ^(){
//? ? ? ? return 100;
//? ? };
//? ? // block調用時沒有參數也要加上括號
//? ? NSLog(@"%d", block());
?? ?
? ? /// 4.有參數有返回值
//? ? NSString *(^block)(NSArray *, NSInteger) = ^(NSArray *arr, NSInteger index){
//? ? ? ? return arr[index];
//? ? };
//? ? NSLog(@"%@", block(@[@"1", @"2"], 1));
?? ?
? ? //寫?個 返回值為整型參數為NSString(僅?個參數)的block,實現將字符串轉換為整型的功能。
? ? //返回整數,參數字符串
//? ? NSInteger (^block)(NSString *) = ^(NSString *str){
//? ? ? ? return str.integerValue;
//? ? };
// ? ?
//? ? NSLog(@"%ld", block(@"23436tsd245"));
?? ?
? ? //參數日期的字符串,返回NSDate類型的日期對象
//? ? NSDate *(^block)(NSString *) = ^(NSString *dateStr){
//? ? ? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//? ? ? ? [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//? ? ? ? return [formatter dateFromString:dateStr];
//
//? ? };
//? ? NSLog(@"%@", block(@"2015.7.28 10.54.23"));
? ? // block的函數段中更多使用參數,而返回值更多是調用一方使用
?? ?
//? ? PFUN p = addNum;
//? ? NSLog(@"%d", p(10, 20));
?? ?
? ? // 通過新的block寫一個block
//? ? Block block = ^(NSArray *arr){
//? ? ? ? return arr[0];
//? ? };
?? ?
? ? // block和局部變量
? ? // __block相當于給a一個在block中修改的權限,否則他只能取值,不能賦值
//? ? __block int a = 10;
//? ? void (^block)() = ^{
//? ? // void (^block)(void) = ^(void){
//? ? ? ? int b = 10 * a ;
//? ? //? a = 10 * a;
//? ? ? ? NSLog(@"%d", a);
//? ? };
//? ? block();
?? ?
? ? // 通過block對數組進行遍歷
//? ? NSArray *arr = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", nil];
//? ? // 系統的吧block
//? ? [arr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
//? ? ? ? NSLog(@"%@", obj);
//? ? }];
// ? ?
? ? // 系統的block的用法就是回車
?? ?
//? ? Person *per = [Person baseWithName:@"媚神" Sex:@"女"];
//? ? NSLog(@"%@", per.name);
?? ?
? ? 數組排序
? ? ///數組對字符串的排序
? ? // 不可變數組
//? ? NSArray *arr = @[@"haoshen", @"meishen", @"wenshen", @"xuanshen", @"shengshen"];
// ? ?
//? ? // SEL是方法選擇器,它是方法選擇器的類型
//? ? NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
//? ? NSLog(@"%@", newArr);
//? ? NSLog(@"%@", arr);
?? ?
? ? // 可變數組
//? ? NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"haoshen", @"meishen", @"wenshen", @"xuanshen", @"shengshen", nil];
// ? ?
//? ? [arr sortUsingSelector:@selector(compare:)];
//? ? NSLog(@"%@", arr);
?? ?
? ? // 對對象的排序
? ? // 新建4個人的對象
//? ? Person *per1 = [Person baseWithName:@"meishen" Sex:@"nv"];
//? ? Person *per2 = [Person baseWithName:@"haoshen" Sex:@"nan"];
//? ? Person *per3 = [Person baseWithName:@"shengshen" Sex:@"nan"];
//? ? Person *per4 = [Person baseWithName:@"jiashen" Sex:@"nan"];
//? ? NSArray *arr = @[per1, per2, per3, per4];
// ? ?
//? ? // 以會員的昵稱作為排序的依據
//? ? NSArray *newArr = [arr sortedArrayUsingComparator:^NSComparisonResult(Person *obj1, Person *obj2) {
//? ? ? ? return -[obj1.name compare:obj2.name]; // return前加上-號,變成逆向排序
//? ? }];
//? ? for (Person *per in newArr) {
//? ? ? ? NSLog(@"%@", per.name);
//? ? }
?? ?
//? ? NSMutableArray *mArr = [NSMutableArray arrayWithObjects:per1, per2, per3, per4, nil];
//? ? [mArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2
//? ? /*mArr sortUsingComparator:^NSComparisonResult(Person *obj1, Person *obj2*/
//? ? ? ? ) {
//? ? ? ? Person *per11 = (Person *)obj1;
//? ? ? ? Person *per22 = (Person *)obj2;
//? ? ? ? return [per11.name compare:per22.name];
// ? ? ? ?
//? ? }];
// ? ?
//? ? for (Person *per in mArr) {
//? ? ? ? NSLog(@"%@", per.name);
//? ? }
? ?return 0;
}
//
//? BaseModel.h
//? OC10_Block
//
//? Created by dllo on 15/7/28.
//? Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BaseModel :NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
- (id)initWithName:(NSString *)name
?? ? ? ? ? ? ? Sex:(NSString *)sex;
+ (id)baseWithName:(NSString *)name
?? ? ? ? ? ? ? Sex:(NSString *)sex;
@end
//
//? BaseModel.m
//? OC10_Block
//
//? Created by dllo on 15/7/28.
//? Copyright (c) 2015年 Clare. All rights reserved.
//
#import "BaseModel.h"
@implementation BaseModel
- (id)initWithName:(NSString *)name
?? ? ? ? ? ? ? Sex:(NSString *)sex
{
? ?self = [superinit];
? ?if (self) {
? ? ? ?self.name = name;
? ? ? ?self.sex = sex;
? ? }
? ? return self;
}
+ (id)baseWithName:(NSString *)name
?? ? ? ? ? ? ? Sex:(NSString *)sex
{
? ? //多態的方式完成對象的創建
? ?id obj = [[[selfclass] alloc]initWithName:name Sex:sex];
? ?return obj;
?? ?
}
@end
//
//? Person.h
//? OC10_Block
//
//? Created by dllo on 15/7/28.
//? Copyright (c) 2015年 Clare. All rights reserved.
//
#import "BaseModel.h"
@interface Person :BaseModel
@end
//
//? Person.m
//? OC10_Block
//
//? Created by dllo on 15/7/28.
//? Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Person.h"
@implementation Person
@end
總結
以上是生活随笔為你收集整理的iOS前期OC训练OC_10Block的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zbrush 4R7中的SubTool该
- 下一篇: 《Android源码设计模式解析与实战》