将字符串对象添加到 NSMutableArray 中?

发布于 2024-08-04 22:25:43 字数 1150 浏览 11 评论 0原文

我正在玩一个小基础工具测试(Objective-C),我有几个问题......

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int planetLoop;
    NSString *tempString;
    NSMutableArray *planetArray = [[NSMutableArray alloc] init];

    NSLog(@"STRING ARRAY ... Start");
    for(planetLoop=0; planetLoop<10; planetLoop++) {
        tempString = [NSString stringWithFormat: @"Planet_%03d", planetLoop+1];
        NSLog(@"Planet_%03d", planetLoop+1);
        [planetArray addObject:tempString];
    }

    [planetArray release];
    [pool drain];
    return 0;
}

首先,通常我在将对象添加到数组后释放它,但我认为我所拥有的是否正确目前是正确的,因为“tempString”是字符串文字,因此不需要分配或释放?

其次,当我运行此命令(在执行之前)时,我会收到以下错误“无法读取未知的加载命令 0x80000022”,如果这是我的代码的问题?从谷歌搜索看来这可能是 xCode 3.1.2 中的一个错误,有人有什么想法吗?

最后我做错了什么,想法是用 10 个字符串“Planet_001”到“Planet_010”填充一个数组

编辑:啊,我明白了,那是因为“= [NSString”位,即

// Autoreleased object
tempString = [NSString stringWithFormat: @"Planet_%03d", planetLoop+1];
// String literal
tempString = @"Planet_"; 

非常感谢,非常感谢 -gary -

I have a little foundation tool test (Objective-C) that I am playing with and I have a few questions ...

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int planetLoop;
    NSString *tempString;
    NSMutableArray *planetArray = [[NSMutableArray alloc] init];

    NSLog(@"STRING ARRAY ... Start");
    for(planetLoop=0; planetLoop<10; planetLoop++) {
        tempString = [NSString stringWithFormat: @"Planet_%03d", planetLoop+1];
        NSLog(@"Planet_%03d", planetLoop+1);
        [planetArray addObject:tempString];
    }

    [planetArray release];
    [pool drain];
    return 0;
}

First, usually I release an object after adding it to an array, but am I right in thinking that what I have currently is right because "tempString" is a string literal, and as such does not need to be allocated or released?

Secondly, when I run this (prior to execution) I get the following eror "unable to read unknown load command 0x80000022" if this a problem with my code? from searching on google it looks like it might be a bug in xCode 3.1.2, anyone have any ideas?

Finally anything I am doing wrong, the idea is to fill an array with 10 string "Planet_001" through to "Planet_010"

EDIT: Ah, I see, thats because of the "= [NSString" bit i.e.

// Autoreleased object
tempString = [NSString stringWithFormat: @"Planet_%03d", planetLoop+1];
// String literal
tempString = @"Planet_"; 

many thanks, much appreciated -gary-

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

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

发布评论

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

评论(3

反目相谮 2024-08-11 22:25:43

tempString 实际上并不是字符串文字。 @"Planet_%03d" 字符串文字。 tempString 是一个自动释放的对象,这意味着当 NSAutoreleasePool 耗尽时它将被释放。基本上,内存已经被管理了,你不需要做任何事情。

规则是:如果您new分配复制保留一个对象,然后你必须释放它。否则,内存已经被管理,可能是通过自动释放来管理的。

此外,您忘记释放。除此之外,看起来还不错。

tempString isn't actually a string literal. @"Planet_%03d" is a string literal. tempString is an autoreleased object, meaning that it will be released when the NSAutoreleasePool is drained. Basically, the memory is already managed and you don't have to do anything.

The rule is: If you new, alloc, copy or retain an object, then you have to release it. Otherwise, the memory is already managed, probably by an autorelease.

Also, you forgot to release pool. Other than that, it looks fine.

活雷疯 2024-08-11 22:25:43

“无法读取未知加载命令 0x80000022”错误的一个可能原因似乎是我已升级到 Snow Leopard,但没有同时升级开发人员工具。看起来该错误可能是由于尝试在 10.6 环境中使用 10.5 版本的 XCode 进行编译而导致的。明天我会调查一下。

Xcode 3.2 现已在 Snow Leopard (Mac OS X 10.6) 版本中提供。安装 Snow Leopard 后,通过与 Xcode Tools 磁盘映像分开安装来升级到 Xcode 3.2。您可以在以前版本的 Xcode 上安装它,或者在安装之前将它们移到一边。

PS:当我收到“无法读取未知加载命令 0x80000022”错误时,我正在运行 OSX 10.6.1 和 xCode 3.1.2

欢呼 -gary-

One possible reason for the "unable to read unknown load command 0x80000022" error appears to be that I've upgraded to Snow Leopard without upgrading the developers tools at the same time. It looks like the error might be caused by trying to use the 10.5 version to XCode to compile in a 10.6 environment. I will look into this tomorrow.

Xcode 3.2 is now available in the Snow Leopard (Mac OS X 10.6) release. After installing Snow Leopard, upgrade to Xcode 3.2 by installing it separately from the Xcode Tools disk image. You can install it over prior versions of Xcode, or move them aside prior to installing.

PS: When I got the "unable to read unknown load command 0x80000022" error I was running OSX 10.6.1 with xCode 3.1.2

cheers -gary-

孤千羽 2024-08-11 22:25:43

该“加载命令”错误是由于可执行格式已从 iPhone OS 3.0 更改为 iPhone OS 3.1。

http://networkpx.blogspot.com/2009/09/about- lcdyldinfoonly-command.html

That "load command" error is due to the fact that the executable format has changed from iPhone OS 3.0 to iPhone OS 3.1.

http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文