数据从txt文件传输到NSArray [英] data from txt file to NSArray

查看:99
本文介绍了数据从txt文件传输到NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是以下事情:

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

I'm using XCode 4.6.1, building an iOS App

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

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

用户名:用户标识:真实名称:客户端类型:客户端版本:纬度:经度:数字:另一个数字:

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

因此,数据用:"分隔,并且所有数据都是用户定义的.

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

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

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

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

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

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

用户名:用户标识:实名:客户端类型1:客户端版本:纬度:经度:数字:另一个数字: 用户名:用户ID:真实名称:客户端类型1:客户端版本:纬度:经度:数字:另一个数字:

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

如何分隔每个用户的数据(所以每行,因为每行是不同的用户).这样我就可以使用用户名,用户名等. 作为示例,按纬度绘制位置.和lon.在以用户名作为标题的地图上.我知道如何绘制,做注释等.这就是如何获取这些数据.

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.

我在想一种读取firstClients数组行的方法,该行将每种类型的数据添加到另一个数组中.用以下方式:userNameArray,useridArray等. 这样,我可以获取每个数组的数据.但是如何做到这一点.

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.

欢迎任何帮助!

推荐答案

确定找到了我的解决方案.如果有更好的弹出窗口,请提出建议.

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

使用此:

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.

这篇关于数据从txt文件传输到NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆