gpt4 book ai didi

ios - 从数组索引 obj c 加载某些信息

转载 作者:行者123 更新时间:2023-11-29 10:45:32 25 4
gpt4 key购买 nike

我有一个数组,可以像这样从 plist 加载信息:

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:@"items.plist"];

这是 plist 的样子:

<array>
<dict>
<key>string</key>
<string>STR</string>
<key>id</key>
<string>0</string>
</dict>
<dict>
<key>string</key>
<string>STR2</string>
<key>id</key>
<string>1</string>
</dict>
<dict>
<key>string</key>
<string>STR3</string>
<key>id</key>
<string>2</string>
</dict>
<dict>
<key>string</key>
<string>STR4</string>
<key>id</key>
<string>35</string>
<key>data</key>
<string>5</string>
</dict>
</array>

我还创建了一个 for 循环,它为 plist 中的每个字符串创建一个 UIButton,如果索引包含“数据”键,我该如何检查循环?我想检查索引是否有数据,以便我可以为按钮设置图像。

最佳答案

像这样的东西应该可以工作:

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:@"items.plist"];
for (NSDictionary * dict in array) {
UIButton * newBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[newBtn setTitle:dict[@"id"] forState:UIControlStateNormal];

// -- Update for comment -- //
// See if data exists
if (dict[@"data"]) {
// if data's value is an image ...
[newBtn setImage:dict[@"data"] forState:UIControlStateNormal];

// In your question the values for @"data" are integers, so make sure you don't try to add that as an image.

}

// do other stuff with button ...

}

关于ios - 从数组索引 obj c 加载某些信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22546950/

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