草庐IT

ios - 区分 UILabel 的元数据时出现问题?

coder 2024-01-25 原文

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

虽然这没关系,但如果我有 2 个不同样式的标签,一个写着“FRANK SINATRA”,而另一个写着“纽约主题,纽约”,那看起来会好得多。

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

    _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;

就像那样我能够将两者分开,问题是当电台进入商业广告时我的应用程序立即崩溃。以下是我在 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

最佳答案

你可能得到没有连字符的输入,所以连字符的 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/

有关ios - 区分 UILabel 的元数据时出现问题?的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  5. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  6. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

随机推荐