2015-06-23 28 views
1

我通過componentsSeparatedByString將字符串轉換爲數組。它返回數組完美。但是當字符串爲空時返回1個對象。componentsSeparatedByString在字符串爲空時返回1個對象

爲什麼會發生這種情況?

NSMutableArray *imagesList=[[[productDetail objectForKey:@"productImage"] componentsSeparatedByString:@","]mutableCopy]; 
+0

你想在這種情況下返回什麼? –

+0

@SergiiMartynenkoJr 0對象。 –

+0

我想知道爲什麼會發生這種情況? –

回答

4

這是因爲,要傳遞空字符串(「」)和componentsSeparatedByString正試圖通過逗號將您的字符串(,)分隔但他們在你的字符串是沒有逗號(,),所以它返回1個陣列項目(即「」)。

NSMutableArray *imagesList = [[NSMutableArray alloc]init];  
if(![productDetail isEqualToString:@""]) { 
    imagesList=[[[productDetail objectForKey:@"productImage"] componentsSeparatedByString:@","]mutableCopy]; 
} 
1

如果在字符串中找不到分隔符,則原始字符串將返回到數組中。