【问题标题】:Issue when differentiating metadata for UILabel?区分 UILabel 的元数据时出现问题?
【发布时间】:2013-11-07 03:33:10
【问题描述】:

我正在获取当前在我的 iOS 应用中播放的广播流的元数据(艺术家姓名、曲目名称)。但是,此元数据是一体的,即 =“FRANK SINATRA - 来自纽约,纽约的主题”。

虽然这没关系,但如果我有 2 个不同风格的标签,其中一个写着“FRANK SINATRA”,而另一个写着“THEME FROM NEW YORK, NEW YORK”,那就更好看了。

为此,我必须去掉连字符,

    _metaData = infoString;   // THIS IS THE OLD 1- LABEL FOR  DATA ALL WAY

    // THIS IS THE CODE I AM IMPLEMENTING TO GET RID OF THE HYPHEN

    NSUInteger xxx = [_metaData rangeOfString:@"-"].location;


    NSUInteger xxxPlustTwo = xxx + 2;

    NSString *xxxx = [_metaData substringToIndex:xxx];

    self.artistName.text=xxxx;

    NSString *trackInformation = [_trackName substringFromIndex:xxxPlustTwo];

    self.soundInfoMeta.text = trackInformation;

就像我能够将 2 分开一样,问题是,当电台开始商业化时,我的应用程序会立即崩溃。以下是我在 Xcode 上收到的错误: ***Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘***-[__NSCFString substringToIndex:]: Index 2147483647 out of bounds; string length 17’

什么可能导致这个问题?

更新:我尝试将 NSUInteger 更改为 double 但这并没有解决问题,这是最新的崩溃日志:

xxxx    NSString *  nil 0x00000000
NSObject    NSObject        
isa Class   0x0 
xxx double  2147483647  2147483647
xplus   double  2147483649  2147483649
_trackName  __NSCFString *  @"http://www.181.fm"    0x16d8a890
NSObject    NSObject        
isa Class   __NSCFString    0x39d9a8f8

【问题讨论】:

    标签: ios objective-c nsstring substring


    【解决方案1】:

    您可能收到没有连字符的输入,所以连字符的locationNSNotFound,我相信它被定义为NSIntegerMax。您的代码需要对此保持稳健。

    这与你现在的做法有点不同,但它应该适用于更多类型的输入:

    NSString *infoString = @"ARTIST - TRACK";
    NSArray *infoStringComponents = [infoString componentsSeparatedByString:@" - "];
    __block NSString *artistString = @"";
    __block NSString *trackInfoString = @"";
    
    if ([infoStringComponents count] == 0) {
        // This case should only happen when there's no info string at all
        artistString = @"Unknown Artist";
        trackInfoString = @"Unknown Song";
    }
    else if ([infoStringComponents count] == 1) {
        // If no hyphens just display the whole string as the track info
        trackInfoString = infoStringComponents[0];
    }
    else {
        [infoStringComponents enumerateObjectsUsingBlock:^(NSString *component, NSUInteger index, BOOL *stop) {
            NSString *trimmedComponent = [component stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if (index == 0) {
                artistString = trimmedComponent;
            }
            else {
                NSString *buffer = @"";
                if ([trackInfoString length] > 0) {
                    buffer = @" - ";
                }
                trackInfoString = [trackInfoString stringByAppendingFormat:@"%@%@",buffer,trimmedComponent];
            }
        }];
    }
    

    您还可以检查您派生的范围的location 属性是否等于NSNotFound,然后假设您无法从中派生艺术家并在适当的标签中显示您的_metaData 变量。例如:

    NSRange hyphenRange = [infoString rangeOfString:@"-"];
    if (hyphenRange.location == NSNotFound) {
        // Display only infoString to the user, unformatted into artist / song info
    }
    else {
        // Try the same technique you're attempting now
    }
    

    【讨论】:

    • 多么棒的答案,这是我在 SO 得到的最好的答案。如果我能给你我所有的代表,我完全愿意。感谢您抽出宝贵的时间!真的很感激
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多