2013-08-25 23 views
0

這裏的任何一個人都有一個想法,我可以將這個數組分成2?如何將NSArray中的內容分解爲Objective C中的兩個新內容?

2013-08-25 02:47:47.052 yahoo[11357:c07] (
"", 
"1377253260000.33300.0", 
"1377253440000.33280.0", 
"1377254100000.33280.0", 
"1377255600000.33220.0", 
"1377257400000.33220.0", 
"1377261660000.33200.0", 
"1377264000000.33200.0", 
"1377264060000.33200.0", 
"1377267780000.33200.0", 
"1377271260000.33200.0", 
"1377273120000.33200.0", 
"1377273180000.33200.0", 
"1377273240000.33240.0", 
"" 
) 

第一個NSArray會與長數字和第二個與較小的包括「。」。

所以像這樣:array1與1377253260000和array2與33300.0等。

+1

如何快速'$ man sscanf' – CodaFi

+0

感謝您的回覆,但那是什麼?它是客觀的C嗎? –

回答

1

有很多不同的方式來做到這一點。例如,你可以做簡單的找到的第一個週期,加起來串到那個時期的第一陣列中,一切都下一個陣列中後一句:

NSMutableArray *smallerNumbers = [NSMutableArray array]; 
NSMutableArray *longNumbers = [NSMutableArray array]; 

for (NSString *string in array) { 
    NSRange range = [string rangeOfString:@"."]; 
    if (range.location != NSNotFound) { 
     [longNumbers addObject:[string substringToIndex:range.location - 1]]; 
     [smallerNumbers addObject:[string substringFromIndex:range.location + 1]]; 
    } else { 
     [longNumbers addObject:@""]; // or you could insert [NSNull null] or whatever 
     [smallerNumbers addObject:@""]; 
    } 
} 
+0

解決了這個問題! –

0

另一種方式..

NSArray *objects = @[ 
        @"", 
        @"1377253260000.33300.0", 
        @"1377253440000.33280.0", 
        @"1377254100000.33280.0", 
        @"1377255600000.33220.0", 
        @"1377257400000.33220.0", 
        @"1377261660000.33200.0", 
        @"1377264000000.33200.0", 
        @"1377264060000.33200.0", 
        @"1377267780000.33200.0", 
        @"1377271260000.33200.0", 
        @"1377273120000.33200.0", 
        @"1377273180000.33200.0", 
        @"1377273240000.33240.0", 
        @"" 
        ]; 

NSMutableArray *firstParts = [[NSMutableArray alloc] initWithCapacity:objects.count]; 
NSMutableArray *secondParts = [[NSMutableArray alloc] initWithCapacity:objects.count]; 

for (NSString *object in objects) 
{ 
    NSArray *components = [object componentsSeparatedByString:@"."]; 

    if (components.count > 0) { 
     [firstParts addObject:components[0]]; 
    } 
    if (components.count > 1) { 
     [secondParts addObject:components[1]]; 
    } 
} 
NSLog(@"firstParts = %@", firstParts); 
NSLog(@"secondParts = %@", secondParts); 
相關問題