nsstrings 的 NSMutableArray (在 UISearchBar 委托之一中)可以使用持久存储吗?
我想在我的应用程序中创建一个历史列表。我在这里浏览并查看了很多问题,但到目前为止我还没有找到我的问题的答案。
我的代码:
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog (@"search bar text did end editing!");
NSString *bookmarked = mySearchBar.text;
NSMutableArray *bookmarkl = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < [bookmarkl count]; i ++)
{
[bookmarkl addObject:bookmarked];
}
for (NSString *sa in bookmarkl);
NSLog (@"whole array: %@", bookmarkl);
}
但我每次得到的只是一个字符串。怎么了? 我想向可变数组添加一个新字符串,我应该怎么做?
I would like to make a history list in my app. I have surfed and looked at many questions here, but so far I haven´t found the answer to my question.
My codes:
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog (@"search bar text did end editing!");
NSString *bookmarked = mySearchBar.text;
NSMutableArray *bookmarkl = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < [bookmarkl count]; i ++)
{
[bookmarkl addObject:bookmarked];
}
for (NSString *sa in bookmarkl);
NSLog (@"whole array: %@", bookmarkl);
}
But all I get is one string at a time. What is wrong?
I want to add a new string to the mutable array, how should I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用该函数时,您都会创建一个新列表。您应该将 bookmarkl 作为属性添加到您的类中,并且仅在初始化期间创建一次。如果您希望在应用程序关闭时保留历史记录,您还需要将数据写入文件。您可以使用 [NSMutableArray arrayWithContentsOfFile] 加载历史记录并使用 writeToFile 保存它。
You are creating a new list every time the function is called. You should add bookmarkl to your class as a property and only create it once during initialization. If you want the history to persist when your app is closed you will also want to write the data to a file. You can use [NSMutableArray arrayWithContentsOfFile] to load the history and writeToFile to save it.