如何从NSMutableArray访问NSObject类变量进行排序
我已将几个class Objects
添加到NSMutableArray
中。它似乎工作,但我想现在访问我已经存储到数组中的类对象内的变量,但我现在遇到一些错误。如何从NSMutableArray访问NSObject类变量进行排序
起初我是我传递NSDictionary
这NSArray
/太是走在字典中的每个条目,并将其放置到正确类型的变量的方法的所有NSString
基于目的条目的NSDictionary
。然后这个变量的这个NSObject
被传回到它被调用的地方。
下面是我用来实现此目的的代码。
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循环
SearchResultList.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;
}
从这里
所以,我结束回到回复for回复.m我在那里调用searchObjectArray addObject:searchResultList];
来捕获返回的类对象。
在这一点上,我有两个问题。
首先,我是不是正确捕获的NSObject
类
二,一旦我添加了所有的类对象的数组的我怎么可能再存取权限的特定对象的变量数组中?
我问第二个问题的原因是因为我想将此数组传递给排序方法,该排序方法基于数组中对象的一个或多个变量进行排序。
任何帮助将不胜感激。
NSLog(@"%@", myobj.doorID);
第一个问题:
非常感谢你..这帮了我一大堆!它的工作完美...和一个奖金,因为我明白了最近怎么回事:)感谢阅读量和昨天的尝试......但再次感谢您的帮助! – HurkNburkS 2012-08-13 20:35:24
您可以通过调用该行代码访问一个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;
是的,这是我的想法,但是当我尝试我得到一个错误出现,说。 **在'NSObject *'类型的对象上没有找到'doorID'属性,但是在调试时查看对象显示这个值在我正在登录的对象中显示出来。 – HurkNburkS 2012-08-13 04:57:34
如果我添加了'NSObject * myobj = [searchObjectArray objectAtIndex:0]; NSLog(@“%@”,myobj);'在for循环之后,当我将鼠标移到myobj上时添加一个断点我可以看到所有变量及其值可用..我只是不知道如何单独访问它们... – HurkNburkS 2012-08-13 04:08:39