简体   繁体   English

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

[英]Adding string objects to NSMutableArray?

I have a little foundation tool test (Objective-C) that I am playing with and I have a few questions ... 我正在玩一个基础工具测试(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;
}

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? 首先,通常我在将对象添加到数组后释放对象,但是我是否认为当前的对象是正确的,因为“ tempString”是字符串文字,因此不需要分配或释放吗?

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? 其次,当我运行此代码(执行之前)时,如果我的代码有问题,则会得到以下错误“无法读取未知的加载命令0x80000022”。 from searching on google it looks like it might be a bug in xCode 3.1.2, anyone have any ideas? 从谷歌搜索似乎是xCode 3.1.2中的错误,任何人有任何想法吗?

Finally anything I am doing wrong, the idea is to fill an array with 10 string "Planet_001" through to "Planet_010" 最后我做错了什么,这个想法是用10个字符串“ Planet_001”到“ Planet_010”填充一个数组

EDIT: Ah, I see, thats because of the "= [NSString" bit ie 编辑:啊,我明白了,就是因为“ = [NSString”位,即

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

many thanks, much appreciated -gary- 非常感谢,非常感谢-gary-

tempString isn't actually a string literal. tempString实际上不是字符串文字。 @"Planet_%03d" is a string literal. @"Planet_%03d" 字符串文字。 tempString is an autoreleased object, meaning that it will be released when the NSAutoreleasePool is drained. tempString是自动释放的对象,这意味着将在耗尽NSAutoreleasePool时将其释放。 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. 规则是: 如果您newalloccopyretain一个对象,则必须release它。 Otherwise, the memory is already managed, probably by an autorelease. 否则,可能已经通过自动释放来管理内存。

Also, you forgot to release pool . 另外,您忘记释放 pool Other than that, it looks fine. 除此之外,它看起来还不错。

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. “无法读取未知的加载命令0x80000022”错误的一个可能原因似乎是我已升级到Snow Leopard,而没有同时升级开发人员工具。 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. 看来该错误可能是由于尝试使用10.5版本的XCode在10.6环境中进行编译引起的。 I will look into this tomorrow. 我明天会调查这个。

Xcode 3.2 is now available in the Snow Leopard (Mac OS X 10.6) release. Snow Leopard(Mac OS X 10.6)发行版中现已提供Xcode 3.2。 After installing Snow Leopard, upgrade to Xcode 3.2 by installing it separately from the Xcode Tools disk image. 安装Snow Leopard后,通过与Xcode Tools磁盘映像分开安装来升级到Xcode 3.2。 You can install it over prior versions of Xcode, or move them aside prior to installing. 您可以在Xcode的早期版本上安装它,也可以在安装之前将它们移到一旁。

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 PS:当出现“无法读取未知的加载命令0x80000022”错误时,我正在使用xCode 3.1.2运行OSX 10.6.1。

cheers -gary- 欢呼声-加里

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. 该“加载命令”错误是由于以下事实导致的:可执行格式已从iPhone OS 3.0更改为iPhone OS 3.1。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM