从txt文件到NSArray的数据

时间:2022-03-18 02:23:14

What I would like to do is the following:

我想做的是以下内容:

I'm using XCode 4.6.1, building an iOS App

我正在使用XCode 4.6.1,构建一个iOS应用程序

I have an textfile (.txt) that contains my data. It is fetched from a server. The data it contains is like this:

我有一个包含我的数据的文本文件(.txt)。它是从服务器获取的。它包含的数据是这样的:

username:userid:realname:clienttype:clientversion:latitude:longitude:number:anothernumber:

So data is seperated by ":" and all data is user defined.

因此,数据由“:”分隔,所有数据都是用户定义的。

Because there are 2 types of clients: clienttype1 and clienttype2 I've devided the .txt file into an array using

因为有两种类型的客户端:clienttype1和clienttype2我已经将.txt文件分成了一个数组

NSArray *dataClient = [datafile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; //to load txt file line for line in NSArray

NSIndexSet *clientindex = [data1 indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    NSRange range = [(NSString *)obj rangeOfString:@":clienttype1:"];
    if (range.location != NSNotFound)
    {
        return YES;
    }
    return NO; // Set the index

NSArray *firstClients = [dataClient objectsAtIndexes:clientindex]; // create array with clienttype1 only

Now I have an array with only objects of clienttype1 data. As this:

现在我有一个只包含clienttype1数据对象的数组。这样:

username:userid:realname:clienttype1:clientversion:latitude:longitude:number:anothernumber: username:userid:realname:clienttype1:clientversion:latitude:longitude:number:anothernumber:

How can I separate the data per user (so per line, cause each line is a different user). So that I can use username, userid etc. As an example plot location by lat. and lon. on a map with username as title. I know how to plot, make annotations etc. It is just how to get to that data.

如何为每个用户分隔数据(因此每行,因为每行是不同的用户)。这样我就可以使用用户名,用户ID等。作为示例,通过lat绘制位置。和lon。在用户名作为标题的地图上。我知道如何绘制,制作注释等。这就是如何获取数据。

I was thinking of a way to read the firstClients array line for line to add each type of data into a different array. In the way of: userNameArray, useridArray etc. In this way I can fetch data per Array. But how to do this.

我正在考虑一种方法来读取行的firstClients数组行,以将每种类型的数据添加到不同的数组中。顺便提一下:userNameArray,useridArray等。通过这种方式,我可以获取每个数组的数据。但是如何做到这一点。

Any help is welcome!

欢迎任何帮助!

1 个解决方案

#1


0  

Ok found my solution. If anything better pops up please advise.

好的找到我的解决方案如果有什么更好的弹出请告知。

Used this:

for (int i = 0; i < [firstClients count]; i++) {
    NSString *oneLine = [atcClients objectAtIndex:i];

    NSArray *oneLineSeparated = [oneLine componentsSeparatedByString:@":"]; 
}

Now I got an Array with: username, userid, realname etc.

现在我得到了一个包含:username,userid,realname等的数组。

#1


0  

Ok found my solution. If anything better pops up please advise.

好的找到我的解决方案如果有什么更好的弹出请告知。

Used this:

for (int i = 0; i < [firstClients count]; i++) {
    NSString *oneLine = [atcClients objectAtIndex:i];

    NSArray *oneLineSeparated = [oneLine componentsSeparatedByString:@":"]; 
}

Now I got an Array with: username, userid, realname etc.

现在我得到了一个包含:username,userid,realname等的数组。