OGeek|极客世界-中国程序员成长平台

标题: ios - 区分 UILabel 的元数据时出现问题? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:09
标题: ios - 区分 UILabel 的元数据时出现问题?

我正在获取当前在我的 iOS 应用中播放的广播流的元数据(艺术家姓名、轨道名称)。然而,这个元数据是一体的,即 = “FRANK SINATRA - THEME FROM NEW YORK, NEW YORK”。

虽然这没问题,但如果我有 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 上收到的错误: ***由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[__NSCFString substringToIndex:]: Index 2147483647 out of bounds;字符串长度 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



Best Answer-推荐答案


您可能收到没有连字符的输入,因此连字符的 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
}

关于ios - 区分 UILabel 的元数据时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827532/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4