iOS之个人工作常用知识


数组篇

//  将数组重复的对象去除,只保留一个
+ (NSArray *)arrayWithMemberIsOnly:(NSArray *)array
{
NSMutableArray *categoryArray = [NSMutableArray alloc] init];
for (unsigned i = 0; i < [array count]; i++) {
@autoreleasepool {
if ([categoryArray containsObject:[array objectAtIndex:i] == NO) {
[categoryArray addObject:[array objectAtIndex:i];
}
}
}
return categoryArray;
}
把 + 换成 -,不然self是调用不了的


//数组排序

//第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象

NSComparator cmptr = ^(id obj1, id obj2){
 if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
 
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
};

NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
 //排序前
 NSMutableString *outputBefore = [[NSMutableString alloc] init];
 for(NSString *str in sortArray){
  [outputBefore appendFormat:@"];
 }
 NSLog(@"排序前:%@",outputBefore);
 [outputBefore release];
 //第一种排序
 NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
 
 NSMutableString *outputAfter = [[NSMutableString alloc] init];
 for(NSString *str in array){
  [outputAfter appendFormat:@"];
 }
 NSLog(@"排序后:%@",outputAfter);
 [outputAfter release];


第二种 排序方法 利用sortedArrayUsingFunction 调用 对应方法customSort,这个方法中的obj1和obj2分别是指数组中的对象。
NSInteger customSort(id obj1, id obj2,void* context){
 if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
 
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}

 NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
 //排序前
 NSMutableString *outputBefore = [[NSMutableString alloc] init];
 for(NSString *str in sortArray){
  [outputBefore appendFormat:@"];
 }
 NSLog(@"排序前:%@",outputBefore);
 [outputBefore release];
 
 NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];
 
 NSMutableString *outputAfter = [[NSMutableString alloc] init];
 for(NSString *str in array){
  [outputAfter appendFormat:@"];
 }
 NSLog(@"排序后:%@",outputAfter);
 [outputAfter release];


第三种 利用sortUsingDescriptors调用NSSortDescriptor

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:NO];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便
  NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
  [_totalInfoArray sortUsingDescriptors:sortDescriptors];
  [_airListView refreshTable:_totalInfoArray];
  [sortDescriptor release];
  [sortDescriptors release];

第三种只是说了一些语法使用,下面给出一个详细的例子

self.logisticModel.infos = {

        {
        action = "\U60a8\U63d0\U4ea4\U4e86\U8ba2\U5355\Uff0c\U8bf7\U7b49\U5f85\U7cfb\U7edf\U786e\U8ba4";
        day = "2015-12-16";
        time = "11:52:48";
    },
        {
        action = "\U60a8\U7684\U8ba2\U5355[31201512162000055]\U786e\U8ba4\U6709\U6548";
        day = "2015-12-16";
        time = "11:51:26";
    },
        {
        action = "\"\U77f3\U5bb6\U5e84\U5e02:\U6295\U9012\U5e76\U7b7e\U6536\Uff0c\U7b7e\U6536\U4eba\Uff1a\U672c\U4eba\U6536\"";
        day = "2015-07-03";
        time = "17:43:00";
    },
        {
        action = "\"\U77f3\U5bb6\U5e84\U5e02:\U672a\U59a5\U6295\Uff0c\U539f\U56e0\Uff1a\U6536\U4ef6\U4eba\U4e0d\U5728\U6307\U5b9a\U5730\U5740\"";
        day = "2015-07-02";
        time = "18:17:00";
    }
} //这个数组里面存放的是一些模型对象

 logisticsInfosArray = [[NSMutableArray alloc]init];
        if (self.logisticModel.infos.count > 0) {
            for (int i = 0 ; i < self.logisticModel.infos.count; i++) {
                NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
                self.logisticInfoModel = self.logisticModel.infos[i];//这个数组里面存放的是一些模型对象
                
                if (self.logisticInfoModel.time.length != 0) {
                    NSArray *timeTempArray = [self.logisticInfoModel.time componentsSeparatedByString:@" "];
                    [tempDict setObject:timeTempArray[0] forKey:@"day"];
                    [tempDict setObject:timeTempArray[1] forKey:@"time"];
                }
                
                if (self.logisticInfoModel.action.length != 0) {
                    [tempDict setObject:self.logisticInfoModel.action forKey:@"action"];
                }
                
                [logisticsInfosArray addObject:tempDict];
            }
            NSSortDescriptor *daySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"day" ascending:NO];
            NSSortDescriptor *timeDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];
            NSArray *logisticsArray = [NSArray arrayWithObjects:daySortDescriptor,timeDescriptor, nil];
            [logisticsInfosArray sortUsingDescriptors:logisticsArray];


这样最后就返回了一个先按照天来排序,然后再按照time来排序的数组了


字符串NSString篇

  NSString *myString5 = @"   one     two three  ";


NSString *trimmed1 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];

        //whitespaceAndNewlineCharacterSet去除前后的空格和换行符

        NSString *trimmed2 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];

        //whitespaceCharacterSet 去除前后的空格

         NSLog(@"myString5 = %@",myString5);

         NSLog(@"trimmed1 = %@",trimmed1);

         NSLog(@"trimmed2 = %@",trimmed2);

2015-12-24 14:26:59.670 test[62689:7687169] myString5 =    one     two three  
2015-12-24 14:26:59.670 test[62689:7687169] trimmed1 = one     two three
2015-12-24 14:26:59.670 test[62689:7687169] trimmed2 = one     two three


去除全部空格

    NSPredicate *noE = [NSPredicate predicateWithFormat:@"SELF!=''"];
    
    NSArray *part = [myString5 componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"part:%@",part);
    NSArray *file = [part filteredArrayUsingPredicate:noE];
    NSLog(@"file:%@",file);
    
    NSString *trimmed3 = [file componentsJoinedByString:@""];
    
    NSLog(@"trimmed3 = %@",trimmed3);}

打印结果

2015-12-24 14:14:29.611 test[62637:7680204] part:(
    "",
    "",
    "",
    one,
    "",
    "",
    "",
    "",
    two,
    three,
    "",
    ""
)
2015-12-24 14:14:29.611 test[62637:7680204] file:(
    one,
    two,
    three
)
2015-12-24 14:14:29.612 test[62637:7680204] trimmed3 = onetwothree

tableView(Cell)篇

1.动态改变cell的高度

-(CGFloat)cellHeightForTextView:(NSString *)cellSTr{
    MyMessageCell *cell = [self.editAddressTableView dequeueReusableCellWithIdentifier:myMessageCellIdentifer];
    cell.nameTextField.text = cellSTr;
    CGSize cellSize = [cell.nameTextField sizeThatFits:CGSizeMake(cell.nameTextField.frame.size.width, FLT_MAX)];
    CGFloat defaultHeight = cell.contentView.frame.size.height;
    CGFloat height = cellSize.height > defaultHeight ? cellSize.height : defaultHeight;
    return height + 1;

}
-(CGFloat)cellHeightForLabel:(NSString *)cellSTr{
    MyAddressAreaCell *cell = [self.editAddressTableView dequeueReusableCellWithIdentifier:myAreaCellIdentifer];
    cell.selectAreaLabel.text = cellSTr;
    CGSize cellSize = [cellSTr boundingRectWithSize:CGSizeMake(cell.selectAreaLabel.frame.size.width, FLT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cell.selectAreaLabel.font} context:nil].size;

    CGFloat defaultHeight = cell.contentView.frame.size.height;
    CGFloat height = cellSize.height > defaultHeight ? cellSize.height : defaultHeight;
    return height + 1;
    
}



本文出自 “冯宋林的博客” 博客,谢绝转载!

你可能感兴趣的:(工作技能(数组))