ios - 将 UITextfield 问题转换为 NSMutableArray

标签 ios

我使用 NSArray 进行了这项工作,直到我意识到我需要将一个字符串插入到数组中。现在我将其更改为 NSMutableArray,但我的语法有问题。我可以在 NSMutable 数组中使用 componentsJoinedByString 吗?

    NSString *item;
    int numofSeg;
    int needSeg;
if (textfield.text == @"::") {
        numofSeg = 0;

    }
    NSMutableArray *dlist = [[NSMutableArray alloc]init];
    //take string from textfield and split it into a list on colons.
    dlist = [textfield.text componentsSeparatedByString:@":"];
    //run through list to count how many segments. Non blank ones.
    for (item in dlist) {
        if (item != @""){
            numofSeg = numofSeg + 1;
            NSLog(@"%@",numofSeg);
        }
    }
    //determine number of segments
    needSeg = 8 - numofSeg;
    while (needSeg > 0) {
        //insert 0000 at blank spot
        [dlist insertString:@"0000" atIndex:@""];
        needSeg = needSeg - 1;
    }
    for (item in dlist) {
        if (item == @"") {
            //remove blank spaces from list
            [dlist removeAllObjects:item];
        }
    }
    //join the list of times into a string with colon
    NSString *joinstring = [dlist componentsJoinedByString:@":"];
    NSLog(@"%@",joinstring);

最佳答案

我认为 NSMutableArray 没有名为 insertString:atIndex: 的方法。您可能应该改用 insertObject:atIndex:

你到底得到了什么错误?

编辑:

如果你只使用 NSMutableArray@"" 之前插入 @"0000",那么你可以简单地做:

string = [string stringByReplacingOccurrencesOfString:@" " withString:@" 0000"];

或类似的东西。无需使用 NSMutableArray

关于ios - 将 UITextfield 问题转换为 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960770/

相关文章:

ios - 获取以米为单位的 MKMapView 边界

ios - 在我的应用程序中创建一个目录来存储不同类型的文件

ios - Multipeer Connectivity 存储以前 session 中的 PeerID

javascript - React Native 不显示来自 TextInput 的日期选择器

ios - Swift 3 中的 rac_textSignal

ios - ionic 4 : The "src" content of an "img" tag is not updating when I change it from the controller in IOS

ios - iTunesU 笔记 API 还是文件备份?

ios - 无法构建模块基础

android - 用于使用远程服务器数据的应用程序的 native 或 Web 移动应用程序?

ios - 复制 Storyboard 时,界面生成器文件中的未知类