iOS如何使用searchString填充NSMutableArray?

时间:2012-07-27 22:16:40

标签: ios search nsmutablearray uisearchbar

当用户点击“x”按钮到一个可变数组时,我想从UISearchBar收集所有searchString,但是它没有添加任何对象到self.historyList ....

在方法中:

            - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchString
        {
            self.ss = searchBar.text;

//this is initialized in viewDidLoad method
            //self.addSSFort = [[NSMutableArray alloc]init];

            [self.addSSFort addObject: self.ss];

            NSLog (@"addSSFort contains %@", self.addSSFort);

            //NSMutableArray *historyList = [[[NSMutableArray alloc]init]retain];
//this one is initialized in viewDidLoad method
            //self.historyList = [[NSMutableArray alloc]init];

            if ([self.ss length] == 0)
            {
        // here it is never being added
                [self.historyList addObject:[self.addSSFort objectAtIndex:[self.addSSFort count]-1]];
            }
    }

编辑:

self.addSSFort contains:

x
xc
xco
xcod
xcode <- I want to add this to self.historyList
""
t
tr
tre
tree <- I want to add this to self.historyList
""

但是self.historyList包含:

""
""

我希望它包含:

xcode
tree

我使用此语句if ([self.ss length] == 0),因为它告诉我用户点击“x”来删除searchString并告诉我他/她已完成当前的searchString。 我知道ARC,但我正在为拥有iOS 3&gt;的用户这样做。 iOS 5。

编辑2:

啊,我已经解决了这个问题!

3 个答案:

答案 0 :(得分:0)

这是你的例子的伪代码:

get text
add text to array
if text is empty string:
    add empty string to history

这真的是你想要的吗?

答案 1 :(得分:0)

也许我不明白你想做什么,但你有

If ([self.ss length] == 0)
{
        [self.historyList addObject:[self.addSSFort objectAtIndex:[self.addSSFort count]-1]];
}

如果搜索栏中有文本,则self.ss的长度不会为0.您的“If”语句会阻止将对象添加到self.historyList的行。您是否在将字符串对象添加到historyList的行上放置了一个断点,看看你是否到达那里?

作为旁注,如果您正在使用ARC,则不需要保留。

答案 2 :(得分:-1)

应该是:

[self.historyList addObject:[self.addSSFort objectAtIndex:[self.addSSFort count]-2]];

而不是:

[self.historyList addObject:[self.addSSFort objectAtIndex:[self.addSSFort count]-1]];