2012-09-03 50 views
-1

我有一個問題,ios到php EXC_BAD_ACCES數據對象

我需要將此發送到一個php文件。 只有我得到一個EXC_BAD_ACCES。

也許是因爲我需要釋放一個數據對象。 但我不知道我的數據對象是什麼。

我從http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html複製了這個。

也許你可以幫助我,它可能是一個很小的變化,但我沒有看到它。

電賀

NSURL *url = [NSURL URLWithString:@"http://www.yourdomain.com/locatie.php"]; 

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestReloadIgnoringCacheData 
                 timeoutInterval:60]; 

[theRequest setHTTPMethod:@"POST"]; 
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

NSString *postData = [NSString stringWithFormat:@"longitude=%@&latitude=%@&stringFromDate=%@", longitude, latitude, stringFromDate]; 

NSString *length = [NSString stringWithFormat:@"%d", [postData length]]; 
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"]; 

[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]]; 

NSURLConnection *sConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self]; 
[sConnection start]; 

if (sConnection) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    theRequest = [[NSMutableData data] retain]; 
} else { 
    // Inform the user that the connection failed. 
} 


} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
// This method is called when the server has determined that it 
// has enough information to create the NSURLResponse. 

// It can be called multiple times, for example in the case of a 
// redirect, so each time we reset the data. 

// receivedData is an instance variable declared elsewhere. 
} 
- (void)connection:(NSURLConnection *)connection 
didFailWithError:(NSError *)error 
{ 
// release the connection, and the data object 
[connection release]; 
// receivedData is declared as a method instance elsewhere 

// inform the user 
NSLog(@"Connection failed! Error - %@ %@", 
     [error localizedDescription], 
     [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
// do something with the data 
// receivedData is declared as a method instance elsewhere 
NSLog(@"Succeeded! Received bytes of data"); 

// release the connection, and the data object 
[connection release]; 
} 
+0

上線你明白了EXC_BAD_ACCES? – sergio

+0

這也是我的問題。它不會給出這個錯誤。我只是覺得我需要釋放數據對象。我剛剛刪除了數據對象行,因爲我不知道它在哪裏,數據對象是什麼 –

回答

0

Enable NSZombies,以獲取有關崩潰的更多的診斷信息,。 這很可能與過度釋放對象有關。

看你的代碼,我覺得現在的問題是在這裏:

NSURLConnection *sConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self]; 

這是一個自動釋放的對象,將在其聲明範圍的結束而消失(這個方法的)。

此修復程序將NSURLConnection引用存儲在您的類的retain屬性中。