奇怪的NSFormatter行为

时间:2015-06-12 15:02:17

标签: objective-c cocoa

我有TextField,其值绑定到Document的serverAddress属性(readwrite,copy),TextField formatter委托与 XIB 中的ServerAddressFormatter对象相关联。< / p>

它实际上正在处理像127.0.0.1:8080这样的输入,但是只要我放了一些没有: TextField的内容就完全清楚了。

此处ServerAddressFormatter实施:

@implementation ServerAddressFormatter

- (NSString *) stringForObjectValue:(NSArray *)obj {
    if ([obj isKindOfClass:[NSArray class]]) {
        return [obj componentsJoinedByString:@":"];
    } else {
        return @"";
    }
}

- (BOOL)getObjectValue:(out id *)anObject
             forString:(NSString *)string
      errorDescription:(out NSString **)error {
    int i;
    for (i = (int) ([string length] == 0 ? 0 : [string length] - 1); i > 0; i--) {
        if ([string characterAtIndex:i] == ':') break;
    }

    if (i == 0) {
        *anObject = @[string]; // if I put string, @"100" here it's working fine
    } else {
        *anObject = @[[string substringToIndex:i], [string substringFromIndex:i+1]];
    }

    return YES;
}
@end

0 个答案:

没有答案