基本上,问题标题说明了一切。我尝试了很多东西,但都不起作用。
我有扩展 NSURLSession 的类别,用于处理服务器和网络错误
- (NSURLSessionDataTask *)
dataTaskHandlingErrorsWithRequest:(NSURLRequest *)request
completionHandler:(void (^)(NSData *))completionHandler {
return
[self dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response,
NSError *error) {
if (!error && response &&
[response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
if (resp.statusCode / 100 == 2) {
completionHandler(data);
} else {
// Wrong status code from server.
NSNotificationCenter *center =
[NSNotificationCenter defaultCenter];
[center postNotificationName:kPANotifiNameServerStatusError
object:self
userInfo:@{
@"response" : resp
}];
NSLog(@"Wrong status code");
completionHandler(nil);
}
} else {
// Something wrong with network.
NSNotificationCenter *center =
[NSNotificationCenter defaultCenter];
[center postNotificationName:kPANotifiNameNetworkError
object:self
userInfo:@{
@"error" : error ? error : [NSNull null]
}];
NSLog(@"Internet connection problem.");
completionHandler(nil);
}
}];
}
这是我调用它的地方:
- (void)authenticateWithHandler:(AuthHandler)handler {
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task =
[session dataTaskHandlingErrorsWithRequest:self.request
completionHandler:^(NSData *data) {
if (data) {
BOOL suc = [self handleResponse:data];
handler(suc);
} else {
handler(NO);
}
}];
[task resume];
}
因此,在 iOS 7.1 上,如果我调用该方法,它会抛出
2015-08-06 15:29:50.973 MyApp[2618:607] -[__NSCFURLSession dataTaskHandlingErrorsWithRequest:completionHandler:]: unrecognized selector sent to instance 0x7871bf40
2015-08-06 15:29:50.976 MyApp[2618:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFURLSession dataTaskHandlingErrorsWithRequest:completionHandler:]: unrecognized selector sent to instance 0x7871bf40'
但在 iOS 8 上它可以工作。我检查了编译源,那里一切正常。
最佳答案
你能检查你的类别方法吗:
if ([self isKindOfClass:[NSURLSession class]]) {
NSLog(@"YES");
}
也许您尝试在其上运行它的对象只是被转换为 NSURLSession 而实际上它不是..?只是一些猜测,因为我仍然不知道。
我想在这里点Mattt answer ,这有助于了解其工作原理。
关于ios - 从类别调用方法在 iOS 7.1 中给出 "Unrecognised selector error",但在 8.4 中没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31854760/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案