将 NSArray 中的 NSDictionary 中的 NSString 放入 UIActionSheet 中

发布于 2024-11-25 07:52:27 字数 1716 浏览 1 评论 0原文

我正在使用 NSUserDefaults 保存 NSMutableArray。然后在另一个视图控制器中,我将从 NSUserDefaults 中检索 NSMutableArray。所以我想要做的是从该数组中的第一个 NSDictionary 检索 NSString,然后将其作为按钮添加到我的 UIActionSheet 中。然后我会循环它,因此从 NSMutableArray 的第二个 NSDictionary 中检索第二个 NSString。我还想让它们保持正确的顺序。那么 NSDictionary 中的 NSString(NSMutableArray 中的 objectAtIndex:0)是 UIActionSheet 中的第一个按钮吗? 这可能吗?如果可能的话,我将如何实现这一目标?

非常感谢!

编辑(此代码中有泄漏)。 Xcode 说它在粗体行中,但我不确定,我是否在这里泄漏了任何内容或做错了什么?

NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
**NSMutableArray *accountsArray = [[prefs objectForKey:@"AccountArray"] mutableCopy];**
if ([accountsArray count] >= 2) {

    UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:@"Hi?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [playingSheet showInView:self.view];
    for(NSDictionary * dict in accountsArray)
    {
        NSString *name = [dict objectForKey:@"Name"];
        [playingSheet addButtonWithTitle:name];
    }
    [playingSheet release];
}

编辑2: 当我运行以下代码时在应用程序中运行分析时收到警告。无论如何,错误是,粗体行中的对象可能泄漏并存储到accountsArray中。该行是 edit2 帖子中的第二行。

NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
**NSMutableArray *accountsArray = [[prefs objectForKey:@"AccountArray"] mutableCopy];**
if ([accountsArray count] >= 2) {

UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:@"Hi?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[playingSheet showInView:self.view];
for(NSDictionary * dict in accountsArray)
{
    NSString *name = [dict objectForKey:@"Name"];
    [playingSheet addButtonWithTitle:name];
}
[playingSheet release];
}

I am saving a NSMutableArray using NSUserDefaults. Then in another view controller, I would retrieve the NSMutableArray from the NSUserDefaults. So what I want to do is retrieve the NSString from the first NSDictionary in that array, then add it as a button to my UIActionSheet. Then I would loop it, so retrieve the second NSString from the second NSDictionary from that NSMutableArray. I would also like to keep them in the right order. So the NSString from the NSDictionary that is objectAtIndex:0 in the NSMutableArray, is the first button in the UIActionSheet?
Is this possible, and if so how would I accomplish this?

Thanks so much!

Edit (There is a leak in this code). Xcode says its in the bolded line but I am not sure, am I leaking anything here or doing anything wrong?

NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
**NSMutableArray *accountsArray = [[prefs objectForKey:@"AccountArray"] mutableCopy];**
if ([accountsArray count] >= 2) {

    UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:@"Hi?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [playingSheet showInView:self.view];
    for(NSDictionary * dict in accountsArray)
    {
        NSString *name = [dict objectForKey:@"Name"];
        [playingSheet addButtonWithTitle:name];
    }
    [playingSheet release];
}

Edit2:
I get a warning when I run analyze in my app when I run the following code. Anyway the error is, Potential leak of an object in the bolded line and stored into accountsArray. That line is the second line in the edit2 post.

NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
**NSMutableArray *accountsArray = [[prefs objectForKey:@"AccountArray"] mutableCopy];**
if ([accountsArray count] >= 2) {

UIActionSheet *playingSheet = [[UIActionSheet alloc] initWithTitle:@"Hi?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[playingSheet showInView:self.view];
for(NSDictionary * dict in accountsArray)
{
    NSString *name = [dict objectForKey:@"Name"];
    [playingSheet addButtonWithTitle:name];
}
[playingSheet release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2024-12-02 07:52:27

像这样的东西吗?

UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle .......]


for(NSDictionary * dict in myMutableArray)
{
    NSString * str = [dict objectForKey:@"MyStringKey"];
    [sheet addButtonWithTitle:str];
}

Something like this?

UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle .......]


for(NSDictionary * dict in myMutableArray)
{
    NSString * str = [dict objectForKey:@"MyStringKey"];
    [sheet addButtonWithTitle:str];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文