在NSLog中格式化NSString和Display的数组

时间:2014-12-19 02:54:31

标签: objective-c arrays nsstring nsarray

我已将Core Data属性分类为基于Entity Material 的数组和一个属性 category 的数组,并希望在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每个类别中显示“Category:%@,arrayWithCatNames”?

更新

我发现了我的问题的答案,并在下面发布。

2 个答案:

答案 0 :(得分: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代码。现在工作正常。

答案 1 :(得分: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方法中将其用作数据源,也可以在获取期间从核心数据中获取已排序的类别。如果您希望我为您做这件事,请告诉我。