我有一个具有多个属性的核心数据实体,我想要一个属性中所有对象的列表。我的代码如下所示:
2 3 4 5 6 7 8 9 10 11 12 | let context:NSManagedObjectContext = appDel.managedObjectContext! let sortDesc = NSSortDescriptor(key:"username", ascending: true) let fetchReq = NSFetchRequest(entityName:"Identities") fetchReq.sortDescriptors = [sortDesc] fetchReq.valueForKey("username") let en = NSEntityDescription.entityForName("Identities", inManagedObjectContext: context) userList = context.executeFetchRequest(fetchReq, error: nil) as [Usernames] |
但这给了我一个 NSException 错误,我不知道为什么,或者我应该怎么做。我已经阅读了 NSFetchRequest 类的描述,但无法理解它。
任何建议都将不胜感激。
编辑:收到 Bluehound 的提示后,我将代码更改为:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @IBAction func printUsers(sender: AnyObject) { let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context:NSManagedObjectContext = appDel.managedObjectContext! let sortDesc = NSSortDescriptor(key:"friendID", ascending: true) let fetchReq = NSFetchRequest(entityName:"Identities") fetchReq.sortDescriptors = [sortDesc] fetchReq.propertiesToFetch = ["friendID"] let en = NSEntityDescription.entityForName("Identities", inManagedObjectContext: context) userList = context.executeFetchRequest(fetchReq, error: nil) as [Model] println(userList) } |
运行时错误消失了,但我仍然不知道它是否有效,因为我不确定如何将列表转换为字符串列表。
一如既往,我们将不胜感激。
有两种可能:可以发出正常的fetch请求
并从结果中提取包含所需属性的数组,
使用
2 3 4 5 6 7 8 9 10 | fetchReq.sortDescriptors = [sortDesc] var error : NSError? if let result = context.executeFetchRequest(fetchReq, error: &error) as? [Model] { let friendIDs = map(result) { $0.friendID } println(friendIDs) } else { println("fetch failed: \\(error!.localizedDescription)") } |
斯威夫特 2:
2 3 4 5 6 7 8 9 10 | fetchReq.sortDescriptors = [sortDesc] do { let result = try context.executeFetchRequest(fetchReq) as! [Model] let friendIDs = result.map { $0.friendID } print(friendIDs) } catch let error as NSError { print("fetch failed: \\(error.localizedDescription)") } |
或者您将
和
在这种情况下,获取请求将返回一个字典数组:
2 3 4 5 6 7 8 9 10 11 12 | fetchReq.sortDescriptors = [sortDesc] fetchReq.propertiesToFetch = ["friendID"] fetchReq.resultType = .DictionaryResultType var error : NSError? if let result = context.executeFetchRequest(fetchReq, error: &error) as? [NSDictionary] { let friendIDs = map(result) { $0["friendID"] as String } println(friendIDs) } else { println("fetch failed: \\(error!.localizedDescription)") } |
斯威夫特 2:
2 3 4 5 6 7 8 9 10 11 12 | fetchReq.sortDescriptors = [sortDesc] fetchReq.propertiesToFetch = ["friendID"] fetchReq.resultType = .DictionaryResultType do { let result = try context.executeFetchRequest(fetchReq) as! [NSDictionary] let friendIDs = result.map { $0["friendID"] as! String } print(friendIDs) } catch let error as NSError { print("fetch failed: \\(error.localizedDescription)") } |
第二种方法的优点是只有指定的属性
从数据库中获取,而不是从整个托管对象中获取。
它的缺点是结果不包含pending
托管对象上下文中未保存的更改(
使用
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我最近开始学习Ruby,这是我的第一门编程语言。我对语法感到满意,并且我已经完成了许多只教授相同基础知识的教程。我已经写了一些小程序(包括我自己的数组排序方法,在有人告诉我谷歌“冒泡排序”之前我认为它非常聪明),但我觉得我需要尝试更大更难的东西来理解更多关于Ruby.关于如何执行此操作的任何想法?
我在Ruby中遇到了一个关于Dir[]和File.join()的简单程序,blobs_dir='/path/to/dir'Dir[File.join(blobs_dir,"**","*")].eachdo|file|FileUtils.rm_rf(file)ifFile.symlink?(file)我有两个困惑:首先,File.join(@blobs_dir,"**","*")中的第二个和第三个参数是什么意思?其次,Dir[]在Ruby中有什么用?我只知道它等价于Dir.glob(),但是,我对Dir.glob()确实不是很清楚。 最佳答案
1.回顾.TransportServicepublicclassTransportServiceextendsAbstractLifecycleComponentTransportService:方法:1publicfinalTextendsTransportResponse>voidsendRequest(finalTransport.Connectionconnection,finalStringaction,finalTransportRequestrequest,finalTransportRequestOptionsoptions,TransportResponseHandlerT>
目录一.大致如下常见问题:(1)找不到程序所依赖的Qt库version`Qt_5'notfound(requiredby(2)CouldnotLoadtheQtplatformplugin"xcb"in""eventhoughitwasfound(3)打包到在不同的linux系统下,或者打包到高版本的相同系统下,运行程序时,直接提示段错误即segmentationfault,或者Illegalinstruction(coredumped)非法指令(4)ldd应用程序或者库,查看运行所依赖的库时,直接报段错误二.问题逐个分析,得出解决方法:(1)找不到程序所依赖的Qt库version`Qt_5'
当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#