单击按钮时增加 NSArray
我正在逐行读取文本文件,并将其存储到 NSArray 中。 我想要实现的是当用户单击按钮时增加 NSArray 的索引。
代码:
text.editable = NO;
NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"firstyea" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]; // reads file into memory as an NSString
NSArray *lines = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
NSString *wat = [lines objectAtIndex:1];
text.text = wat;
[self.view addSubview:text];
因此,每当用户单击按钮时,我都必须将 objectAtIndex
加 1。 我可以在 C++ 中通过使用 int
类型的变量并说出类似 objectAtIndex:counter++
的内容来完成此操作,但我不知道如何在 IOS 中执行相同的操作。
I'm reading a text file line by line, and storing it into a NSArray.
What I want to achieve is to increment the index of my NSArray when the user clicks a button.
Code:
text.editable = NO;
NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"firstyea" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]; // reads file into memory as an NSString
NSArray *lines = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
NSString *wat = [lines objectAtIndex:1];
text.text = wat;
[self.view addSubview:text];
So I have to increment objectAtIndex
by 1 whenever the user clicks a button.
I can do it in C++ by having a variable of type int
, and saying something like objectAtIndex:counter++
but I don't know how to do the same thing in IOS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能用 NSArray 做到这一点,你需要使用 NSMutableArray
您应该采用
static int
类型的变量。例如:
注意:使用完数组后不要忘记释放它!
You can't do it with NSArray, you need to use a NSMutableArray
You should take a variable of type
static int
.For example:
Note: Don't forget to release the array after you're finished using it!
全局声明一个整数 i。
@property int i
;在
,声明
i=0
;希望这有帮助。
Declare an integer i globally.
@property int i
;in
, declare
i=0
;Hope this helps.