0

にいくつか追加しclass ObjectsましたNSMutableArray。うまくいったようですが、配列に保存したクラスオブジェクト内の変数にアクセスしたいのですが、現在いくつかのエラーが発生しています。

最初に、/s のこれを渡すメソッドの目的に基づいたNSDictionaryすべてのエントリがあります。これは、ディクショナリ内の各エントリを取得し、正しい型の変数に配置することです。次に、変数の this は、それが呼び出された場所に戻されます。NSStringNSArrayNSDictionaryNSObject

以下は、これを達成するために使用しているコードです。

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;
}

したがって、ここから、 response.mの for ループに戻ります。ここで呼び出しsearchObjectArray addObject:searchResultList];て、返されたクラス オブジェクトをキャプチャします。

この時点で、2 つの質問があります。

まずNSObjectクラスを正しくキャプチャしていますか

次に、すべてのクラス オブジェクトを配列に追加したら、配列内の特定のオブジェクトの変数にアクセスするにはどうすればよいでしょうか。

2 番目の質問をする理由は、この配列を、配列内にあるオブジェクトの 1 つ以上の変数に基づいて並べ替える並べ替えメソッドに渡したいからです。

どんな助けでも大歓迎です。

4

2 に答える 2

2

SearchResultList最初の質問:ループ内でクラスの新しいインスタンスを割り当て/初期化する必要があります。そうしないと、同じオブジェクトを再利用して上書きするだけです。あなたの質問でNSObjectは、技術的には のサブクラスですがNSObject、実際にはカスタムクラスのインスタンスです。SearchResultItem(ちなみに、SearchResultList の代わりにクラス名 like を使用することをお勧めします。これは、実際にはリストではなく、コードを見ている人を混乱させる可能性があるためです。)

NSMutableArray *searchObjectArray = [[NSMutableArray alloc] initWithCapacity:[filteredArray count]];

for (NSDictionary *obj in filteredArray) {
    SearchResultList *searchResultList = [[SearchResultList alloc] init];
    [searchResultList assignSearchData:obj];
    [searchObjectArray addObject:searchResultList];
}

また、ループに高速反復を使用しているため、 myCount カウンターは必要ありません。高速反復は、ループの各反復で使用するために配列内の次のオブジェクトを引き出すためです。

2 番目の質問では、特定のプロパティにアクセスするために、最初に配列から出てくるオブジェクトをカスタム クラス (SearchResultList) としてキャストする必要があります。例えば:

SearchResultList* myObj = (SearchResultList*)[searchObjectArray objectAtIndex:0];
int doorID = myObj.doorID;
于 2012-08-13T05:03:52.750 に答える
0

次のコード行を呼び出して、NSObject プロパティにアクセスできます。

NSLog(@"%@", myobj.doorID);
于 2012-08-13T04:52:25.803 に答える