标签:
-(void)person -(void)personWithAge; -(void)personWithAge:(int)age; -(void)personWithAge:(int)age name:(NSString *)name;
-(void)blockWithoutParameterAndReturnData { void(^myBlock)(void) = ^ { NSLog(@"This is a block without parameter and returned value"); }; myBlock(); }
-(void)AddParameterAndRestunValuesForBlock { int(^blocknames)(int) = ^(int num) { return num * 30; }; int result = blocknames(2); NSLog(@"%d",result); }
self.age = 20; 自动生成[set stAge:20]; int age = self.age; //有待完善 NSArray *names = @[@“mj”,@”jj”,@“jay”]; // NSArray *names = [[NSArray alloc] initWithObjects:@"mj",@"jj",@"jay", nil]; id money = @20; 自动生成: NSNumber *money1 = [[NSNumber alloc] initWithInt:20];
- (NSInteger)addToN1:(NSInteger)n { NSInteger total = 0; for(int i=1;i<=n;i++) { total +=i; } return total; }
- (NSInteger)addToN2:(NSInteger)n { NSInteger total = 0; total = n*(n+1)/2; return total; }
NSMutableArray+Additions.h // // NSMutableArray+Additions.h // MianShiDemo // // Created by zhanggui on 15/7/19. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import <Foundation/Foundation.h> @interface NSMutableArray (Additions) +(NSMutableArray *)sortValues:(NSMutableArray *)mutableArray; @end NSMutableArray+Additions.m // // NSMutableArray+Additions.m // MianShiDemo // // Created by zhanggui on 15/7/19. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "NSMutableArray+Additions.h" @implementation NSMutableArray (Additions) +(NSMutableArray *)sortValues:(NSMutableArray *)mutableArray { NSComparator finderSort = ^(id string1,id string2) { if ([string1 integerValue]>[string2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; }else if ([string1 integerValue]<[string2 integerValue]) { return (NSComparisonResult)NSOrderedAscending; }else { return (NSComparisonResult)NSOrderedSame; } }; NSMutableArray *arr= [[NSMutableArray alloc] initWithCapacity:[mutableArray count]]; NSArray *sortedArr = [mutableArray sortedArrayUsingComparator:finderSort]; for(int i=0;i<[mutableArray count];i++) { [arr addObject:[sortedArr objectAtIndex:i]]; } return arr; } @end 测试方法: - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:@"5",@"20",@"3",@"60" ,@"50",nil]; NSLog(@"%@",[NSMutableArray sortValues:mutableArray]); } 13、使用Category给所有OC对象增加一个run方法(对象方法),然后在run方法中NSLog打印对象本身: NSObject+Additions.h // // NSObject+Additions.h // MianShiDemo // // Created by zhanggui on 15/7/19. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import <Foundation/Foundation.h> @interface NSObject (Additions) -(void)run; @end NSObject+Additions.m // // NSObject+Additions.m // MianShiDemo // // Created by zhanggui on 15/7/19. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "NSObject+Additions.h" @implementation NSObject (Additions) -(void)run { NSLog(@"%@",self); } @end
NSString+Additions.h // // NSString+Additions.h // MianShiDemo // // Created by zhanggui on 15/7/19. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import <Foundation/Foundation.h> @interface NSString (Additions) /**判断NSString中含有的字母个数*/ -(NSInteger)getNumberOfLetterInString; /**反转字符串*/ -(NSString *)reversalString; //删除开始和结束的字符串 -(NSString *)deleteBackAndForwardBlank; //得到域名 -(NSString *)getDominFromWebLink; @end NSString+Additions.m // // NSString+Additions.m // MianShiDemo // // Created by zhanggui on 15/7/19. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "NSString+Additions.h" @implementation NSString (Additions) -(NSInteger)getNumberOfLetterInString { NSInteger strLength = self.length; int result = (int)strLength; for(int i=0;i<strLength;i++) { char c = [self characterAtIndex:i]; if (c>=‘0‘ && c<= ‘9‘) { //判断是不是数字 result = result-1; NSLog(@"数字有:%c",c); } } return result; } -(NSString *)reversalString { NSMutableString *s = [NSMutableString string]; NSUInteger length = self.length; for(NSUInteger i=length;i>0;i--) { [s appendString:[self substringWithRange:NSMakeRange(i-1, 1)]]; } return s; } -(NSString *)deleteBackAndForwardBlank { NSString *trimmedString = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; return trimmedString; } -(NSString *)getDominFromWebLink { NSString *tempString = [NSString string]; NSRange wwwRange = [self rangeOfString:@"www."]; NSRange comRange = [self rangeOfString:@".com"]; NSRange needRange =NSMakeRange(wwwRange.length+wwwRange.location, comRange.location-wwwRange.location-wwwRange.length); tempString = [self substringWithRange:needRange]; return tempString; } @end
标签:
原文地址:http://www.cnblogs.com/zhanggui/p/4660555.html