我已经在 NSMutableArray
中添加了几个 class Objects
。它似乎有效,但我现在想访问存储在数组中的类对象内的变量,但是我现在遇到了一些错误。
最初我有一个 NSDictionary
条目,这些条目都是 NSString
基于方法的目的我传递这个 的
/s 也是将字典中的每个条目放入正确类型的变量中。然后这个 NSArray
NSDictionaryNSObject
变量被传递回调用它的地方。
下面是我用来实现此目的的代码。
response.m
// initalise NSOject Class
SearchResultList *searchResultList = [[SearchResultList alloc]init];
// Create mutableArray with compacity (compacity is based of the current array of NSDictionarys (entries are strings))
NSMutableArray *searchObjectArray = [[NSMutableArray alloc] initWithCapacity:[filteredArray count]];
// count to track progress of the array
int myCount = 0;
// for loop goes through the array passing each object in the array over to the search class object
for (id obj in filteredArray) {
// Pass current array object over to the NSObject Class Method, this method assigns the entires of the NSDictionary object to the variables of the object class coorect type values
[searchResultList assignSearchData:filteredArray[myCount]];
// This is where I capture the returning NSObject Class (at least I think thats whats happening.
[searchObjectArray addObject:searchResultList];
// increments count
myCount ++;
}
//..
这是在for循环中调用的类方法
搜索结果列表.m
//return is of type, SearchResultList which is the object class itself... not sure if this is 100% correct.
- (SearchResultList *)assignSeriesSearchData:(NSMutableDictionary*)tempDict
{
//add all of the NSDictionary entries into their own variables of the correct type such as
// initalize DOORID - NSInteger
doorID = [[tempDict valueForKey:@"DOORID"] integerValue];
// initalize DOORDESC - NSString
doorDesc = [tempDict valueForKey:@"DOORDESC"];
// initalize DOOROPEN - BOOL
doorOpen = [[tempDict valueForKey:@"DOOROPEN"] boolValue];
// initalize DOORLETTER - char
doorLetter = [[tempDict valueForKey:@"DOORLETTER"] UTF8String];
//...
//then I return the NSObject Class to the place where it was called
return self;
}
因此,从这里开始,我最终回到了 response.m 的 for 循环中,我在其中调用了 searchObjectArray addObject:searchResultList];
来捕获返回的类对象。
此时我有两个问题。
首先,我是否正确捕获了 NSObject
类
其次,将所有类对象添加到数组后,我如何才能访问数组中特定对象的变量?
我问第二个问题的原因是因为我想将此数组传递给排序方法,该方法根据数组中对象的一个或多个变量进行排序。
如有任何帮助,我们将不胜感激。
最佳答案
对于您的第一个问题:您应该在循环中分配/初始化您的 SearchResultList
类的新实例,否则您将只是重复使用和覆盖同一个对象。在你的问题中,你一直提到的是 NSObject
,从技术上讲,它是 NSObject
的子类,但它实际上是你的自定义类的一个实例。 (作为旁注,我建议使用像 SearchResultItem
这样的类名而不是 SearchResultList,因为它实际上不是一个列表,这可能会让查看您的代码的人感到困惑,但我已经离开了它就是你拥有它的方式。)
NSMutableArray *searchObjectArray = [[NSMutableArray alloc] initWithCapacity:[filteredArray count]];
for (NSDictionary *obj in filteredArray) {
SearchResultList *searchResultList = [[SearchResultList alloc] init];
[searchResultList assignSearchData:obj];
[searchObjectArray addObject:searchResultList];
}
另请注意,由于您在循环中使用快速迭代,因此不需要 myCount 计数器,因为快速迭代会提取数组中的下一个对象供您在循环的每次迭代中使用。
对于第二个问题,您需要首先将数组中的对象转换为您的自定义类 (SearchResultList),以便访问特定属性。例如:
SearchResultList* myObj = (SearchResultList*)[searchObjectArray objectAtIndex:0];
int doorID = myObj.doorID;
关于iphone - 如何从 NSMutableArray 访问 NSObject 类变量进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927975/