使用NSScanner扫描多个带衬里的文本文件

时间:2013-06-24 19:24:08

标签: objective-c object nsscanner

我有Java和Android开发经验,现在正在尝试学习Objective-C和iPhone / iPad开发。为了帮助自学,我正在将我为Android制作的应用程序重新编写到iPhone上。

我遇到的具体问题与使用NSScanner类有关。在我的Android应用程序中,我读入了一个txt文件(有多行)并构建了加载到数组中的“成员”对象。每个成员都有一般联系信息,包括名字,姓氏,电话号码,电子邮件,承诺类和专业。这个文本文件是我写的,所以我知道确切的格式。下面是文本文件中一行的示例。

Fawzy   Jake    8144425471  someemail@gmail.com Beta    Criminal Justice & Psych Minor

在Java / android中,我能够通过使用多个scan.Next()然后使用scan.nextLine()来加载所需的结果。任何人都可以帮我指出如何使用NSScanner / objective-c执行类似操作的正确方向吗?具体来说,我想知道如何正确扫描文本文件/字符串,然后用我从字符串中抓取的信息构建我的成员对象。

以下是我一直在处理的代码示例,但由于我不熟悉objective-c,因此无法将我想要的内容逻辑地放入语法中。

- (NSString *)loadFileToString{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"akpsi_contact_list"
                                                     ofType:@"txt"];
NSError *error = nil;
NSString *fileContent = [NSString stringWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding
                                                     error:&error];
if(error)
{
    NSLog(@"ERROR while loading from file: %@", error);
}
return fileContent;
}

-(void)readFileString{
NSScanner *scanner = [NSScanner scannerWithString: self.loadFileToString];
while ([scanner isAtEnd] == NO) {

    //member object
    AKPsiMember *member;

    //temporary variables
    NSString *thisFirstName;
    NSString *thisLastName;
    NSString *thisPhoneNum;
    NSString *thisEmail;
    NSString *thisPledge;
    NSString *thisMajor;

    //scan one line, save 
    [scanner scanUpToString:@" " intoString:&thisFirstName];
    [scanner scanUpToString:@" " intoString:&thisLastName];
    [scanner scanUpToString:@" " intoString:&thisPhoneNum];
    [scanner scanUpToString:@" " intoString:&thisEmail];
    [scanner scanUpToString:@" " intoString:&thisPledge];
    [scanner scanUpToString:@" " intoString:&thisMajor];
    //build member object with temporary variables
    // implementation must continue to next line...
}    

}

也是我的成员对象/类

·H

@interface AKPsiMember : NSObject{
NSString *firstName;
NSString *lastName;
NSString *emailAddress;
NSString *pledgeClass;
NSString *major;
NSString *phoneNum;


}

@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *emailAddress;
@property (nonatomic, strong) NSString *pledgeClass;
@property (nonatomic, strong) NSString *major;
@property (nonatomic, strong) NSString *phoneNum;


@end

我的.m

@implementation AKPsiMember

@synthesize firstName = _firstName;
@synthesize lastName = _lastName;
@synthesize phoneNum = _phoneNum;
@synthesize emailAddress = _emailAddress;
@synthesize pledgeClass = _pledgeClass;
@synthesize major = _major;

- (NSString *)phoneNum:(NSNumber *)num
{
if(num == 0)
{
    NSString *temp1 = [self.phoneNum substringWithRange:NSMakeRange(0, 3)];
    NSString *temp2 = [self.phoneNum substringWithRange:NSMakeRange(3, 6)];
    NSString *temp3 = [self.phoneNum substringWithRange:NSMakeRange(6, 9)];

    NSString *formatNum = [NSString stringWithFormat: @"(%@) %@-%@", temp1, temp2, temp3];
    return formatNum;
}
else{
    return self.phoneNum;
}
}

@end

1 个答案:

答案 0 :(得分:2)

扫描完所有字段后,您应扫描换行符,以便继续下一行:

NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisFirstName];
[scanner scanCharactersFromSet:whitespace intoString:nil];        
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisLastName];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisPhoneNum];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisEmail];
[scanner scanCharactersFromSet:whitespace intoString:nil];
[scanner scanUpToCharactersFromSet:whitespace intoString:&thisPledge];
[scanner scanCharactersFromSet:whitespace intoString:nil];

NSCharacterSet *newLineCharacterSet = [NSCharacterSet newlineCharacterSet];
[scanner scanUpToCharactersFromSet:newLineCharacterSet intoString:&thisMajor];
[scanner scanCharactersFromSet:newLineCharacterSet intoString:nil];

在循环之前创建NSMutableArray。

NSMutableArray *members = [NSMutableArray array];

设置循环内的值并将Object添加到members数组。

AKPsiMember *member = [[AKPsiMember alloc] init];
member.firstName = thisFirstName;
// ... set other properties
[members addObject:member];

当数据集的第一个字段之间有空格时,此代码将中断。显然,这可以分解为方法,并且应该检查结果。