NSString alloc/init 生成“重新定义 myStr"错误....? [英] NSString alloc/init generates 'Redefinition of myStr' error....?

查看:29
本文介绍了NSString alloc/init 生成“重新定义 myStr"错误....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎用这个小小的 iPhone 应用程序产生了很多问题......

I seem to be generating a lot of questions with this one little iPhone app....

我试图用 NSStrings 填充 NSMutableArray,我的工作方式如下:

I am attempting to stuff an NSMutableArray with NSStrings, and I am working like this:

...
NSString *myStr = [[NSString alloc] initWithString:@"Jupiter II"]; 
[txtArray addObject: myStr];
[myStr release];

NSString *myStr = [[NSString alloc] initWithString:@"Could this be OUR Waffles?"];
// in this second block, the line above generates the error: 'Redefinition of myStr'. Why?
[txtArray addObject: myStr];
[myStr release];
...

第一个块没有产生错误,但随后的块(每个字符串对象的内容保存相同)都产生错误,'Redefinition of myStr'.为什么?

The first block generates no errors, but the subsequent blocks (identical save for the content of each string object) all generate the error, 'Redefinition of myStr'. Why?

我知道这将是一个面部护理,但我只需要知道并超越这个!

I know that this is going to be a facepalm, but I just gotta know, and get beyond this!

感谢任何和所有帮助!

亲切的问候,

史蒂夫·奥沙利文

推荐答案

只需从第二个块中删除 NSString*:

Just remove NSString* from the second block:

NSString *myStr = [[NSString alloc] initWithString:@"Jupiter II"]; 
[txtArray addObject: myStr];
[myStr release];

myStr = [[NSString alloc] ...

每次编写类似 NSString *myStr 之类的代码时,您都会声明一个新变量 - 并且变量名称在同一范围内必须不同.但是你可以重用同一个变量来存储不同的对象——就像你在上面的代码中所做的那样

Each time you write something like NSString *myStr you declare new variable - and variable names must be different in the same scope. But you can reuse the same variable to store different objects - as you do in the code above

这篇关于NSString alloc/init 生成“重新定义 myStr"错误....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆