简体   繁体   English

如何从NSMutableArray访问NSObject类变量进行排序

[英]how to access NSObject Class variable from NSMutableArray for sorting

I have added several class Objects into a NSMutableArray . 我已经将几个class Objects添加到NSMutableArray It seems to have worked however I would like to now access the variables that are inside the class object I have stored into the array, however I am now running into some errors. 它似乎已经工作但是我想现在访问我已存储到数组中的类对象内的变量,但是我现在遇到了一些错误。

Initially I have a NSDictionary of entries that are all NSString based the purpose of the method I am passing this NSArray of NSDictionary /s too is to take each entry in the dictionary and place it into a variable of the correct type. 最初,我有一个NSDictionary的条目,这些条目都是NSString基于我传递NSDictionary / s的NSArray的方法的目的也是为了获取字典中的每个条目并将其放入正确类型的变量中。 Then this NSObject of variables is passed back to where it was called from. 然后将这个变量的NSObject传递回调用它的位置。

Below is the code I am using to achieve this. 以下是我用来实现此目的的代码。

response.m 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 ++;

    }

//..

This is the class method that being called inside the for loop 这是在for循环中调用的类方法

SearchResultList.m 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;
}

So from here, I end up back in the for Loop of response.m where I call searchObjectArray addObject:searchResultList]; 所以从这里开始,我最终回到了for循环的response.m ,我调用了searchObjectArray addObject:searchResultList]; to capture the the returning class object. 捕获返回的类对象。

At this point I have two questions . 此时我有两个问题

First , am I capturing the the NSObject Class correctly 首先 ,我正确捕获NSObject

Second , Once I have added all of the Class Objects to the array how could I then acces the variables of a specific object in the array? 其次 ,一旦我将所有类对象添加到数组中,我怎么能访问数组中特定对象的变量?

The reason I ask the second question Is because I am wanting to pass this array to a sorting method, which sorts based off one or more variables of the objects that are in the array. 我问第二个问题的原因是因为我想将这个数组传递给一个排序方法,该排序方法根据数组中对象的一个​​或多个变量进行排序。

Any help would be greatly appreciated. 任何帮助将不胜感激。

To your first question: you should alloc/init a new instance of your SearchResultList class within your loop, otherwise you will just be reusing and overwriting the same object. 对于您的第一个问题:您应该在循环中为您的SearchResultList类分配/初始化一个新实例,否则您将只是重用并覆盖同一个对象。 In your question you keep referring to is as an NSObject , which it technically is, being a subclass of NSObject , but it is really an instance of your custom class. 在你的问题中,你一直指的是作为NSObject ,它在技术上是,作为NSObject的子类,但它实际上是你的自定义类的一个实例。 (As a side note I would suggest using a class name like SearchResultItem instead rather than SearchResultList, since it is not really a list and this could be confusing to someone looking at your code, but I've left it the way you had it.) (作为旁注,我建议使用像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];
}

Also note that since you are using fast iteration for your loop you don't need your myCount counter, since fast iteration pulls out the next object in your array for you to use with each iteration of the loop. 另请注意,由于您对循环使用快速迭代,因此不需要myCount计数器,因为快速迭代会拉出数组中的下一个对象,以便您在循环的每次迭代中使用。

For your second question, you need to first cast the objects coming out of your array as your custom class (SearchResultList) in order to access the specific property. 对于第二个问题,您需要首先将来自数组的对象转换为自定义类(SearchResultList)以访问特定属性。 For example: 例如:

SearchResultList* myObj = (SearchResultList*)[searchObjectArray objectAtIndex:0];
int doorID = myObj.doorID;

您可以通过调用以下代码行来访问NSObject属性:

NSLog(@"%@", myobj.doorID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM