2012-08-16 83 views
0

我有一個NSMutableArray我想將標記|替換成;我該怎麼做?替換nsmutablearray裏面的一個對象

NSMutableArray *paths = [dic valueForKey:@"PATH"]; 
NSLog(@"pathArr ", paths) 
pathArr (
    (
     "29858,39812;29858,39812;29925,39804;29936,39803;29949,39802;29961,39801;30146,39782;30173,39779;30220,39774;30222,39774|30215,39775;30173,39779;30146,39782;29961,39801;29949,39802;29936,39803;29925,39804;29858,39812;29858,39812;29856,39812;29800,39819;29668,39843;29650,39847;29613,39855;29613,39855;29613,39856;29605,39857;29603,39867;29603,39867;29599,39892;29596,39909;29587,39957;29571,40018;29563,40038;29560,40043" 
    ) 
) 

更新

這是我從

NSArray *BusRoute = alightDesc; 
int i; 
int count = [BusRoute count]; 
for (i = 0; i < count; i++) 
{ 
    NSLog (@"BusRoute = %@", [BusRoute objectAtIndex: i]); 
    NSDictionary *dic = [BusRoute objectAtIndex: i]; 
    NSMutableArray *paths = [dic valueForKey:@"PATH"]; 
} 
+0

這只是一個字符串。 – 2012-08-16 06:34:38

+0

是不是一個數組? – sihao 2012-08-16 06:55:19

+0

數組中只有一個對象,它是一個字符串。你的問題是從字符串中刪除這些字符,而不是數組中的對象。 – 2012-08-16 18:07:49

回答

2

提供,在陣列的路徑你的對象是字符串,你可以做到這一點

NSMutableArray *path2=[[NSMutableArray alloc]initWithArray:nil]; 
for (NSObject *obect in path) { 
    for (NSString *string in (NSArray*)obect) { 
     [path2 addObject:[string stringByReplacingOccurrencesOfString:@"|" withString:@","]]; 
    } 

} 

NSLog(@"pathArr %@ ", path2); 

您的陣列路徑包含具有字符串作爲對象另一個數組。 希望這有助於

+0

得到了一個錯誤' - [__ NSArrayM stringByReplacingOccurrencesOfString:withString:]:無法識別的選擇器發送到實例0x91ccc0' – sihao 2012-08-16 06:49:16

+0

你能粘貼你的代碼嗎?你確定你使用字符串對象調用該函數嗎? – Neo 2012-08-16 06:55:18

+0

笏你的意思是?'[path2 addObject:[string stringByReplacingOccurrencesOfString:@「|」這個行得到了錯誤 – sihao 2012-08-16 06:59:04

0

得到了我的路,你可投或轉換路徑的NSString,然後做:

paths = (NSString *) [paths stringByReplacingOccurrencesOfString:@"|" withString:@";"]; 

如果這不工作,創建包含pathArr文本的新的NSString實例,調用replaceOccurrences方法並做相反的轉換

NSMutableString *tempStr = [[NSMutableString alloc] init]; 
for (int i = 0; i < [paths count]; i++) 
{ 
    [tempStr appendString:[path objectAtIndex:i]]; 
} 

然後對tempStr使用此方法。然後嘗試:

NSArray *newPaths = [tempStr componentsSeparatedByString:@";"]; 

可能是最後一種方法不完全正確,因此請嘗試使用它。

+0

得到警告tat說'NSMutableArray可能不會響應stringByReplacingOccurrencesOfString:與String'。 – sihao 2012-08-16 06:51:09

0

//數組複製到一個字符串

的NSString * STR = [路徑componentsJoinedByString:@ 「」];

//然後替換「|」

str = [str stringByReplacingOccurrencesOfString:@「|」 withString:@ 「;」];

0

我這樣做在的.plist替換字符串所以它可能會爲你

array1 = [NSMutableArray arrayWithContentsOfFile:Path1]; 
NSString *item = [@"dfdfDF"]; 
[array1 replaceObjectAtIndex:1 withObject:item]; 

     [array1 writeToFile:Path1 atomically:YES]; 
    NSLog(@"count: %@", [array1 objectAtIndex:1]); 
0

呃工作,你爲什麼不只是去:

NSString *cleanedString = [[[dic valueForKey:@"PATH"] objectAtIndex:0] stringByReplacingOccurrencesOfString:@";" withString:@"|"]; 

如果有超過一個嵌套數組,你可以去

for(int i = 0; i < [[dic valueForKey:@"PATH"] count]; i++) 
{ 
    NSString *cleanedString = [[[dic valueForKey:@"PATH"] objectAtIndex:i] stringByReplacingOccurrencesOfString:@";" withString:@"|"]; 

    // do something with cleanedString 
} 
相關問題