![](/img/trans.png)
[英]iOS app crashing with -[NSCFString count]: when trying to get count of NSMutableArray if count is only 1
[英]App is crashing when NSMutableArray no Objects in iOS
我有四個文本textfield1
,例如textfield1
, textfield2
, textfield3
, textfield4
和一個按鈕。 當我單擊按鈕時,我正在將文本字段文本添加到NSMutableArray
並填充在tableview
。
我的代碼是:
- (void)buttonClick
{
NSMutableArray *array =[[NSMutableArray alloc]init];
[array addObject: textfield1.text];
[array addObject: textfield2.text];
[array addObject: textfield3.text];
[array addObject: textfield4.text];
}
使用委托方法在表格視圖中填充
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[array objectAtIndex:indexPath.row];
return cell;
}
上面的代碼工作正常。 但是,當我單擊按鈕時未在文本字段中輸入任何數據時,由於數組為空,應用程序崩潰。 如何解決這個問題呢?
您不能將nil
添加到數組中。
[array addObject:textfield1.text ?: @""];
[array addObject:textfield2.text ?: @""];
[array addObject:textfield3.text ?: @""];
[array addObject:textfield4.text ?: @""];
這樣可以確保數組中有四個項目(可能是空字符串)。
為什么會崩潰?
您不能在數組中添加nil。 它里面應該有東西。
-(void)buttonClick
{
NSMutableArray *array =[[NSMutableArray alloc]init];
if ([textfield1.text length]>0) {
[array addObject: textfield1.text];
}
if ([textfield2.text length]>0) {
[array addObject: textfield2.text];
}
if ([textfield3.text length]>0) {
[array addObject: textfield3.text];
}
}
嘗試這個
-(void)buttonClick
{
NSMutableArray *array =[[NSMutableArray alloc]init];
if(textfield1.text.length > 0)
[array addObject: textfield1.text];
if(textfield2.text.length > 0)
[array addObject: textfield2.text];
if(textfield3.text.length > 0)
[array addObject: textfield3.text];
if(textfield4.text.length > 0)
[array addObject: textfield4.text];
}
使用委托方法在表格視圖中填充
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[array objectAtIndex:indexPath.row];
return cell;
}
如果您有多個文本字段,則://給出每個文本字段的標簽值從100到150或任何標簽(我正在考慮50個文本字段)
for (int i=100; i<=150; i++) {
// self.view or use ur view on which u r addding textfield
id txtF = [self.view viewWithTag:i];
if([txtF isKindOfClass:[UITextField class]]) {
UITextField *txtField = (UITextField*)txtF;
if(txtField.text.length > 0) {
//Add ur object
}
}
}
您可以先檢查文本字段,然后再放置是否有文本的數組,如果是,則僅在數組中放置文本。
buttonClick方法中的這一行定義了一個本地數組變量:
NSMutableArray *array =[[NSMutableArray alloc]init];
它應該是
array =[[NSMutableArray alloc]init];
這是您的實例變量數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.