2012-02-13 75 views
2

我是iOS開發的新手,我目前正在使用ARC測試RestCit 0.9.3 for iOS和xCode 4.2,我遇到了一些簡單的獲取請求的問題。RestKit iOS:簡單的請求錯誤

我下面這個教程:https://github.com/RestKit/RestKit/wiki/Tutorial-%3A-Introduction-to-RestKit

我嘗試發送一個簡單的GET請求,我的web服務上TouchUpInside一個UIButton

但是我收到一個 「EXC_BAD_ACCESS」:[6373:fb03] *** -[DataAccess respondsToSelector:]: message sent to deallocated instance 0x8275160

在此行中的應用程序停機,對RKRequest.m文件:

if ([self.delegate respondsToSelector:@selector(requestDidStartLoad:)]) { 
    [self.delegate requestDidStartLoad:self]; 
} 

我的代碼:

MyViewController.m:

- (IBAction)myAction:(id)sender { 
    DataAccess *data = [DataAccess alloc]; 
    [data sendRequests]; 
} 
數據訪問:
@implementation DataAccess 

-(void)sendRequests { 

    [RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD]; 
    [[RKClient sharedClient] get:@"/myDistantAction" delegate:self]; 
} 

#pragma mark - Delegate 

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { 

    if ([response isOK]) { 
     NSLog(@"Retrieved : %@", [response bodyAsString]); 
    } 
} 

@end 

我在互聯網上搜索,但我沒有找到解決辦法

有人能幫助我嗎?

謝謝,

+0

你可以嘗試讓數據訪問爲單身? 我認爲這是你的問題 – Beber 2012-02-13 01:11:32

回答

5

這可能是一個解決方案。我將你的代碼改爲使用單例。我認爲問題是當回調函數被調用時,因爲他不能再訪問實例。

DataAccess.m:

@implementation DataAccess 

static singleton *DataAccess= nil; 

+ (DataAccess*)getInstance 
{ 
    if (singleton == nil) { 
     singleton = [[DataAccess alloc] init]; 
    } 
    return singleton; 
} 

-(void)sendRequests { 

    [RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD]; 
    [[RKClient sharedClient] get:@"/myDistantAction" delegate:self]; 
} 

#pragma mark - Delegate 

-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { 

    if ([response isOK]) { 
     NSLog(@"Retrieved : %@", [response bodyAsString]); 
    } 
} 

@end 

MyViewController.m:

- (IBAction)myAction:(id)sender { 
    DataAccess *data = [DataAccess getInstance]; 
    [data sendRequests]; 
} 
+0

它完美的工作!謝謝 – alexmngn 2012-02-13 13:37:06

+0

當我嘗試這個,我得到了錯誤:「未知類型名稱單身人士」,任何想法? – Sebastien 2012-03-05 14:29:49

+0

你的對象名是什麼? – Beber 2012-03-05 15:20:40