2014-12-19 53 views
-3

我已經整理核心數據屬性爲基於實體材料和一個它是一個數組屬性類別並希望顯示的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

我發現了回答我的問題,並貼在下面。

+0

評論是不適合擴展討論;這個對話已經[轉移到聊天](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

回答

0

我從HotLicks的一點點幫助中發現了答案,指着我朝着正確的方向前進。

**我改了名字的數組,以幫助困惑:

// No longer "dictionaries" 
NSArray * arrayOfStrings = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 
id value = [arrayOfStrings valueForKey:@"category"]; 
for (NSString * str in value) { 

    NSLog (@"Category Names: %@",str); 

} 

我補充它在我的代碼的結束和刪除NSString * name代碼。現在工作正常。

0
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 *arrayOfResults = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 
//Convert to String to print better, still not working 
NSArray * categories = [arrayOfResults valueForKey:@"category"]; 

在這裏,你得到的類別陣列,您可以使用它作爲UITableView的數據源的方法,你也可以在取得到有序類別的核心數據。讓我知道你是否希望我爲你做到這一點。