2013-01-22 36 views
1

我不斷收到這個錯誤,當我嘗試將NSObject的綁定到分段控制UISegmentedControl CoreData綁定錯誤

UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60 
2013-01-22 12:44:58.115 Momentum[39936:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UserLocation isEqualToString:]: unrecognized selector sent to instance 0x7477a60' 

我已經驗證了我的核心數據對象有數據。

NSarray *arrayuserlocation = [[MMIStore defaultStore] loadAllUserLocation]; 
UISegmentedControl *segControl = [[UISegmentedControl alloc]initWithItems:arrayuserlocation]; 
[segControl addTarget:self action:@selector(didChangeSegmentControl:)  forControlEvents:UIControlEventValueChanged]; 
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar]; 
[segControl setTintColor:[UIColor grayColor]]; 

編輯

答案下面

- (NSMutableArray *)loadAllUserLocation 
{ 
    if (!allItems) {NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

    NSEntityDescription *e = [[model entitiesByName] objectForKey:@"UserLocation"]; 
      [request setEntity:e] 
    NSError *error; 
     NSArray *result = [context executeFetchRequest:request error:&error]; 
     if (!result) { 
      [NSException raise:@"Fetch failed" 
         format:@"Reason: %@", [error localizedDescription]]; 
     } 

     allItems = [[NSMutableArray alloc] initWithArray:result]; 
} 
return allItems; 

它返回一個數組

我能夠通過執行以下操作來解決我的問題的問題。

NSArray *arraylocation = [[MMIStore defaultStore] loadAllUserLocation]; 
    NSMutableArray *newarray = [[NSMutableArray alloc] init]; 
    for (UserLocation *user in arraylocation) 
    { 

     NSLog(@"%@ found", user.locationNm); 
     [newarray addObject:user.locationNm]; 

    } 

並使用newarray作爲段控制的數據源。

+0

'NSarray arrayuserlocation'應該是'NSarray * arrayuserlocation',對吧?什麼是'loadAllUserLocation'內?看起來它不是NSString。這就是它崩潰的原因。 – iDev

+0

userlocation中有一個int屬性。這是問題嗎? –

+0

根據[文檔](http://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instm/UISegmentedControl/initWithItems :)您的items數組應該是_「一個NSString對象(用於片段標題)或UIImage對象(用於片段圖像)的數組。」_那麼你可以添加完整的'loadAllUserLocation'方法來顯示它的返回類型嗎? – iDev

回答

1

正如我在評論中提到的那樣,問題是您要通過userlocation對象而不是NSStringUIImage所需的對象。

根據documentation您的items數組應該是「一個NSString對象(用於段標題)或UIImage對象(用於段圖像)的數組。

你需要從用戶位置爲取琴絃,

NSarray *arrayuserlocation = [[[MMIStore defaultStore] loadAllUserLocation] valueForKey:@"locationNm"];//use the param name here 

這應該給你的對象數組中的所有字符串數組。

+0

你好,非常感謝你的洞察力。我應該刪除這個問題嗎? –

+0

@KevinKohler,謝謝你的接受。您不必在編輯中完成所有提及的操作。只需使用上面的行。它應該工作。 – iDev

+0

您不必刪除它。編輯後這不是一個壞問題,應該幫助其他面臨類似問題的人。 – iDev

0

問題是,arrayuserlocation數組應包含NSString s而不是NSManagedObjects

0

拋出異常的代碼期望您通過它NSString(這是響應isEqualToString:的對象)。但是,您正在向它傳遞一個UserLocation對象。您需要使用UserLocation對象的字符串加載arrayuserlocation,而不僅僅是發送一個對象數組。

+0

啊,我會怎麼做?它有一個int和字符串屬性? :) –