1

このように、メソッドNSString @"2013-09-02 0:00:00 +0800"を使用して NSArray に分割できます。componentsSeparatedByCharactersInSet

アレイは次のようになります

@["2013-09-02", "0:00:00", "+0800"].

しかし、どのようNSString @"0:00:00"に NSArrayに分割できますか?

@[@"0", "00", "00"] `を使用してObjective-Cで

componentsSeparatedByCharactersInSet方法?

4

4 に答える 4

2

If you want to know the hours, minutes and seconds of a date you should probably use NSDateFormatter first and then extract these properties with NSDateComponents.

NSString *yourString = @"2013-09-02 0:00:00 +0800";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm:ss ZZZZ"];
NSDate *date = [dateFormatter dateFromString:yourString];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:date];
NSInteger hour = components.hour;
NSInteger minute = components.minute;
NSInteger second = components.second;

//Alternative (NSArray can only contain objects - NSNumber vs. NSInteger)
NSArray *yourArray = @[[NSNumber numberWithInteger:components.hour],
                       [NSNumber numberWithInteger:components.minute],
                       [NSNumber numberWithInteger:components.second]];`

It might seem like more code but it is the cleaner solution. And remember NSDateFormatter is relatively expensive to create. If you can do it you should create it outside of a loop (it is not thread safe though);

于 2013-09-06T06:52:14.170 に答える
0
NSArray *arr = [string componentsSeparatedByString:@":"];
于 2013-09-06T06:49:47.793 に答える
0

you have to use componentsSeparatedByString:@":" method.

于 2013-09-06T06:50:42.487 に答える