gpt4 book ai didi

ios - NSMutableArray 索引 1 超出范围 [0 .. 0]

转载 作者:行者123 更新时间:2023-11-29 02:53:17 24 4
gpt4 key购买 nike

我搞砸了 JSON 和 NSMutablearray。我当然是初学者。

我从在线教程中获取并修改了这段代码:

[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 给出了这个:

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!

所以,我认为我正在从数据库中一个接一个地获取数据(“frase uno”,然后是“frase due”,然后是“frase tre”)。而且我还看到数组正在填充(短语存储在数组中:)

当我打电话的时候

prova = [frasiMemorizzate objectAtIndex:0];

NSLog(@"Phrases at index 0: %@",prova);

我得到:“Frase uno”,这是我应该期望的,但是如果我使用“objectAtIndex:1”,我会遇到错误:index 1 beyond bounds [0 .. 0],但我希望有第二个短语:“Frase Due”。

我想知道为什么以及获取我需要的数据的正确方法,也许,当你回答我的问题时,我怎样才能知道有多少项目存储在一个可变数组中,而不是调用 unallocated索引位置!

感谢您的耐心等待!

最佳答案

由于当只有一个对象时尝试访问数组中的第二个索引,因此会引发错误。

做这个小小的改变,它就会起作用:

    prova = [frasiMemorizzate objectAtIndex:i];

NSLog(@"Phrases at index %d: %@", i, prova);

关于ios - NSMutableArray 索引 1 超出范围 [0 .. 0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24228162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com