JSON 및 NSMutablearray가 엉망입니다. 물론 나는 초보자입니다.한계를 넘어 NSMutableArray 인덱스 1 [0 .. 0]
나는 온라인 튜토리얼에서 찍은 내 목적을 위해 수정이 코드를 가지고 :
2014-06-15 11:05:28.705 ProvaMySql[13805:60b] Phrase read from database: Frase uno, Prova!
2014-06-15 11:05:28.706 ProvaMySql[13805:60b] Phrases stored in the array: (
"Frase uno, Prova!"
)
2014-06-15 11:05:28.706 ProvaMySql[13805:60b] Phrases at index 0: Frase uno, Prova!
2014-06-15 11:05:28.707 ProvaMySql[13805:60b] Phrase read from database: Frase due, Prova!
2014-06-15 11:05:28.707 ProvaMySql[13805:60b] Phrases stored in the array: (
"Frase uno, Prova!",
"Frase due, Prova!"
)
2014-06-15 11:05:28.708 ProvaMySql[13805:60b] Phrases at index 0: Frase uno, Prova!
2014-06-15 11:05:28.708 ProvaMySql[13805:60b] Phrase read from database: Frase tre, Prova!!
2014-06-15 11:05:28.709 ProvaMySql[13805:60b] Phrases stored in the array: (
"Frase uno, Prova!",
"Frase due, Prova!",
"Frase tre, Prova!!"
)
2014-06-15 11:05:28.709 ProvaMySql[13805:60b] Phrases at index 0: Frase uno, Prova!
그래서, 나는 하나의 데이터를 얻고 있다고 생각 :
[This is the .m file]
- (void) downloadItems {
// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://example.com/service.php"];
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Initialize the data object
datiScaricati = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the newly downloaded data
[datiScaricati appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Create an array to store the locations
NSMutableArray *frasiMemorizzate = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:datiScaricati options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new location object and set its props to JsonElement properties
Frase *nuovaFrase = [[Frase alloc] init];
nuovaFrase.fraseDelDatabase = jsonElement[@"Frase"];
NSLog(@"Phrase read from database: %@", nuovaFrase.fraseDelDatabase);
// Add this question to the locations array
[frasiMemorizzate addObject:nuovaFrase.fraseDelDatabase];
NSLog(@"Phrases stored in the array: %@", frasiMemorizzate);
prova = [frasiMemorizzate objectAtIndex:0];
NSLog(@"Phrases at index 0: %@",prova);
}
}
NSLog이주는 데이터베이스에서 ("frase uno", "frase due", "frase tre"). 내가
prova = [frasiMemorizzate objectAtIndex:0];
NSLog(@"Phrases at index 0: %@",prova);
를 호출하고 때 내가 얻을 배열 (배열 :
에 저장 표현한 채우는 것을 참조 : "Frase의 UNO"를, 그것이 내가 기대해야하는지,하지만 난 경우 "objectAtIndex : 1"을 사용하십시오. 그 오류가 있습니다 : 경계 1 [0 .. 0]을 넘는 인덱스 1,하지만 두 번째 구 "Frase Due"가있을 것으로 예상됩니다.
내가 필요한 데이터를 얻는 데 필요한 올바른 방법과 방법을 알고 싶습니다. 내 질문에 답하는 중에 어쩌면 많은 항목이 변경 가능 배열에 저장되어 있는지, 호출하지 않는지 알 수 있습니까? 할당되지 않은 색인 위치!
양해 해 주셔서 감사합니다.
감사 Juniperi, 예, 지금은 작동한다! 하지만 세 문구 중 하나만 골라야한다면 어떨까요? 배열에 3 개의 구문을 모두 저장 한 다음 필요한 것을 선택했다고 생각했습니다! – Funesto
@Funesto 당신은 루프를 위해 그 후 원하는 모든 객체를 선택할 수 있습니다. for 루프는 라운드 당 배열에 하나의 객체를 배치하므로 첫 번째 라운드에서 두 번째 객체에 액세스 할 수 없습니다. – juniperi
-_- 대단히 감사합니다. 나는 초보자 만이 아니며 어리 석다! 이제 작동합니다. 당신의 가르침에 감사드립니다! 이제 얼마나 많은 인덱스가 가변 배열에 있는지 알 수있는 방법을 찾아 보겠습니다! – Funesto