我已經整理核心數據屬性爲基於實體材料和一個它是一個數組屬性類別並希望顯示的NSLog存儲的答案。格式排列顯示中的NSLog
這是我用得到我想要的答案代碼:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Materials" inManagedObjectContext:self.managedObjectContext];
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct Categories, only ask for the 'Category' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"Category"]];
fetchRequest.returnsDistinctResults = YES;
NSArray * arrayWithCatNames = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"Categories: %@",arrayWithCatNames);
如果不使用NSString
格式化代碼,我得到這個
2014-12-19 11:28:48.648 App2.0[31085:2652989] Category Names: (
{
category = "Bakelite";
},
{
category = "BOXES, BRACKETS AND BLOCKS";
},
{
category = MISCELLANEOUS;
},
)
我需要它沒有單獨顯示逗號和沒有括號。因爲它排序和存儲的方式與大多數數組不同,所以顯示不正確。我用'NSString * name = [NSString stringWithFormat:...];'在其他一些變化嘗試和更好地排序,但我沒有改善,但它並沒有解決問題。
有些人也有引語,其他人沒有,我沒有保存這種方式。有誰知道爲什麼會發生?謝謝。 這在評論中回答了 - 使用逗號或其他符號(有時甚至是空格)會導致引號。
這是我的問題,從昨日正從核心數據的特定屬性: Displaying Core Data: If attribute has same name display once
我的問題是我怎麼在NSLog
每個類別,如「類別:%@,arrayWithCatNames」顯示?
UPDATE
我發現了回答我的問題,並貼在下面。
評論是不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/67335/discussion-on-question-by-sasmith-formatting-array-of-nsstring-and-display-in-ns) 。 – Taryn 2014-12-19 15:21:44