2014-03-24 42 views
0

我試圖讓用戶角色頁面從登錄中獲取數據。我創建了自己的委託函數來調用web服務,但由於 - [__ NSArrayM setRoleHistorys:]:無法識別的選擇器發送到實例,應用程序崩潰。 這裏是我的代碼:在.m文件:ios錯誤終止 - [__ NSArrayM setRoleHistorys:]:

- (void)parserDidStartDocument:(NSXMLParser *)parser 
{ 
    nodeContent = [[NSMutableString alloc]init]; 
} 

- (void)parserDidEndDocument:(NSXMLParser *)parser 
{ 
    arrayitems = [[NSMutableArray alloc] init]; 
U serRoleDataParser *userroleParser = [[UserRoleDataParser alloc] init]; 
    // UserRole *currentStudent = (UserRole *) arrayitems; 
NSString *Username = username.text; 
NSLog(@"the String value%@",Username); 
[userroleParser getUserHistoryForIndex:0 LoginId:username.text]; 
NSLog(@"the String user value %@",username.text); 
userroleParser.delegate = self; 

} 
- (void) didrecieveData : (NSArray *) userHistories forIndex :(int) index 
{ 
arrayitems = [[NSMutableArray alloc] init]; 
UserRole *roles = (UserRole *) arrayitems; 
roles.RoleHistorys = userHistories; 
datadisplay.text = roles.role; 
NSLog(@"the Success data%@", datadisplay.text); 

} 
在委託文件

.H

@interface UserRole : NSObject 

@property (nonatomic,copy) NSString *username; 
@property (nonatomic,copy) NSString *role; 
@property (nonatomic,copy) NSString *empcode; 

@property (nonatomic,copy)NSMutableArray * RoleHistorys; 
@end 

Dataparser.h文件(代表)

#import <Foundation/Foundation.h> 
#import "UserRole.h" 

@protocol UserRoleDataParserDelegate <NSObject> 

- (void) didrecieveData : (NSArray *) userHistories forIndex :(int) index ; 

@end 

@interface UserRoleDataParser : NSObject<NSXMLParserDelegate> 
{ 
NSMutableData *xmlData; 
NSXMLParser *userroleParser; 
NSMutableString *capturedString; 
BOOL captureCharacters; 
NSMutableArray *userHistories; 
} 

- (void) getUserHistoryForIndex : (int) index LoginId :(NSString*) loginId; 

@property (weak,nonatomic) id <UserRoleDataParserDelegate> delegate; 
@property (nonatomic) int index; 

@end 

我得到的輸出的NSLog,但該應用程序崩潰。

回答

1

此代碼是不正確的:

arrayitems = [[NSMutableArray alloc] init]; 
UserRole *roles = (UserRole *) arrayitems; 

你不能只投一個數組來您的自定義類的類型(除非你有子類NSMutableArray和實例實際上是子類型)。在嘗試使用它之前,您需要創建一個實例或找到您想要的正確實例。

+0

你能告訴我如何添加代碼? –

+0

我不能告訴你需要做什麼。也許只是'UserRole * roles = [[UserRole alloc] init]'。目前還不清楚,因爲你沒有看到已經有一個,你沒有做很多與你試圖使用的(你不存儲它)... – Wain

0

創建NSMutableArray的,你把它轉換到UserRole的對象,在這之後您嘗試將數據保存在:roles.RoleHistorys

arrayitems = [[NSMutableArray alloc] init]; 
UserRole *roles = (UserRole *) arrayitems; 
roles.RoleHistorys = userHistories; //<- Error happened here 

所以你想通過在類型的NSMutableArray對象調用選擇setRoleHistorys保存數據,你不能這樣做,因爲NSMutableArray不包含那個屬性。

+0

雅錯誤發生在那裏..這ü標記.. –

+0

是的,我解釋了爲什麼。可能您想要將UserRole對象添加到數組中,之後您想要從數組中獲取數據並更改對象。但現在你只是試着改變NSMutableArray上的對象,因爲沒有你想要修改的屬性,所以這是行不通的。 – Greg