2011-08-22 39 views
0

hy everyone,用[[object valueForKey:@「」] componentsJoinedByString:@「 n」]添加兩個值;

我真的需要幫助。 這裏是我的問題: 我有對象的數組,每個有兩個屬性: 1是一個NSString名 2是NSSNumber價格

現在我想通過陣列運行,所有的值添加到新字符串:

問題是我需要下面的輸出。 name(from object 1):price(from object 1)then new line(\ n) name(from object 2):price(from object 2)then new line(\ n)...等等。

會很棒,如果有人能幫助我這裏。 與親切的問候,托馬斯

+0

你有你的數組中是什麼樣的對象?你的一個類的實例,一個NSDictionary? – Zoleas

+0

是我的一個課程的實例,對不起,我沒有提到它在第一個地方。感謝您的回覆+ g + – Thomas

回答

3

試試這個:

// This contains your objects with names and prices; 
NSArray *productArray; 

... 

// |listOfProducts| will be your string with all names and prices. 
NSMutableString *listOfProducts = [[NSMutableString alloc] init]; 

// Build the list of products 
for (Product *product in productArray) { 
    [listOfProducts appendFormat:@"%@: $%0.2f\n", product.name, product.price]; 
} 
+0

哦,天啊!非常感謝你。當有人提出這樣一個很好的解決方案時,我總是記得我仍然需要學習很多東西。祝你好運。謝謝托馬斯 – Thomas