Swift-通过块传递结构数组并检索成员 [英] Swift - Pass struct array through block and retrieve members
问题描述
此数据是在viewDidLoad中接收的,例如:
var packArray = [Any]()//该块返回Any覆盖func viewDidLoad(){super.viewDidLoad()BuildArray.buildArrayFromQuery(queryForCollection:"Pack",发件人:self){(结果)在self.packArray = [结果]}}
打印出此self.packArray会在一行上返回以下所有内容,而我现在不知道如何提取数据.
[[静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发件人:__ObjC.UIViewController,completeBlock:(Any)->())->()).(collectionStruct#1)(名称:"pk00",描述:这是一些文字",标题:不是凶手",图片:< PFFile:0x60000005fc80>,id:"rHITAAHJYk"),静态ParseStarterProject_Swift.BuildArray.:Swift.String,发送者:__ObjC.UIViewController,completeBlock:(任何)->())->()).(collectionStruct#1)(名称:"pk01",描述:这是一些文字",标题:成瘾",图像:< PFFile:0x600000240300>,id:"uGHHpPF89e"),静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发送方:__ ObjC.UIViewController,completeBlock :(任何))->())->()).(collectionStruct#1)(名称:"pk02",描述:这是一些文字",标题:毛巾很吵",图像:< PFFile:0x600000240570>,id:"PeM7hJ4sih"),静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发件人:__ObjC.UIViewController,completeBlock:(Any)->())->()).(collectionStruct#1)(名称:"pk03",说明:"This is some text",标题:"Class action",图像:< PFFile:0x6000002407e0>,id:"LoSXT2PFoS"),静态ParseStarterProject_Swift.buildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发件人:__ObjC.UIViewController,completeBlock:(Any)->())->()).(collectionStruct#1)(名称:"pk04",描述:这是一些文本",标题:啤酒",图片:< PFFile:0x600000240a50>,id:"vxEsd13twt"),静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发送者:__ ObjC.UIViewController,completeBlock:(Any)->())->()).(collectionStruct#1)(名称:"pk05",说明:"This is some text",标题:"Not again ...",图片:< PFFile:0x600000240cc0> ;, id:"JNqaAgtdRb"),静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,sender:__ObjC.UIViewController,completeBlock:(Any)->())->()).(collectionStruct#1)(名称:"pk06",描述:这是一些文字",标题:外国汽车",图片:< PFFile:0x600000240f30>,id:"Hb16TDXGbz"),静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发件人:__ ObjC.UIViewController,completeBlock :(任何)->())->()).(collectionStruct#1)(名称:"pk07",描述:这是一些文字",标题:皮肤问题",图像:< PFFile:0x6000002411a0>,id:"MUYDMnJCrU"),静态ParseStarterProject_Swift.buildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发件人:__ObjC.UIViewController,completeBlock:(Any)->())->()).(collectionStruct#1)(名称:"pk08",描述:这是一些文本",标题:垃圾食品",图片:< PFFile:0x600000241410>,id:"yowAfJlcmr"),静态ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery(queryForCollection:Swift.String,发送者:__ ObjC.UIViewController,completeBlock:(任何)->())->()).(collectionStruct#1)(名称:"pk09",描述:这是一些文本",标题:"Delusion",图像:< PFFile:0x600000241680>,id:"TiLAKBPmaD")]]
以前我正在使用:
cell.labelCell.text = self.packArray [indexPath.item] .packDescription
但是现在返回没有成员packDescription.我可能可以使用字符串处理并创建混乱,但是必须有一种更好的方法将数据作为实际数组获取.我知道我将其强制转换为Any,但找不到另一种通过完成块从struct数组中获取数据的方法.
-----编辑----
在创建数组的类中有一个struct,以及来自struct的数组结果以Any的形式通过completeBlock传递
class func buildArrayFromQuery(queryForCollection:字符串,发件人:UIViewController,completeBlock:@转义(_结果:任意)->空白) {struct collectionStruct {var名称:字符串var说明:字符串var title:字符串var image:PFFilevar id:字符串}var collectionArray = [collectionStruct]()
然后从PFQuery中创建它,然后将所得数组通过完成处理程序传回
query.findObjectsInBackground(block:{(objects,error)in如果错误!= nil {打印(错误!)} if let packs = objects {用于包装物品{打印(fromName)打印(对象)让arrayName = object.object(forKey:fromName)as!细绳让arrayDescription = object.object(forKey:fromDescription)为!细绳让arrayTitle = object.object(forKey:fromTitle)为!细绳让arrayImage = object.object(forKey:fromImage)为!PF文件让arrayID = object.objectId作为String!collectionArray.append(collectionStruct(name:arrayName,description:arrayDescription,title:arrayTitle,image:arrayImage,id:arrayID!))}}completeBlock(结果:collectionArray)})
您需要将查询结果转换为所需的类型.如果将packArray设为可选,则可以使用
直接分配 var packArray:[collectionStruct]?self.packArray =结果为?[collectionStruct]
使用该代码,如果不能将结果强制转换为正确的数组类型,则self.packArray将包含nil.
或者您可以使用可选的绑定:
var packArray:[collectionStruct] = []
...
如果structsArray =结果为[collectionStruct] {self.packArray = structsArray}
您应该在viewDidLoad之外定义packArray:
var packArray:[collectionStruct] = []覆盖func viewDidLoad(){super.viewDidLoad()BuildArray.buildArrayFromQuery(queryForCollection:"Pack",发件人:自我){(造成如果structsArray =结果为[collectionStruct] {self.packArray = structsArray}}
您的代码在viewDidLoad内部定义了一个局部变量packArray,它使事情变得混乱.我建议不要使用具有相同名称的实例变量和局部变量.
i have a completion block that hands an struct array of data to another viewcontroller.
This data is received in viewDidLoad like:
var packArray = [Any]()// the block is returning Any
override func viewDidLoad() {
super.viewDidLoad()
BuildArray.buildArrayFromQuery(queryForCollection: "Pack", sender: self) { (result) in
self.packArray = [result]
}
}
printing this self.packArray returns the following all on one line and i now have no idea how i can extract the data.
[[static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk00", description: "This is some text", title: "Not a murderer", image: <PFFile: 0x60000005fc80>, id: "rHITAAHJYk"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk01", description: "This is some text", title: "Addiction", image: <PFFile: 0x600000240300>, id: "uGHHpPF89e"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk02", description: "This is some text", title: "Towels are noisy", image: <PFFile: 0x600000240570>, id: "PeM7hJ4sih"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk03", description: "This is some text", title: "Class action", image: <PFFile: 0x6000002407e0>, id: "LoSXT2PFoS"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk04", description: "This is some text", title: "Beer", image: <PFFile: 0x600000240a50>, id: "vxEsd13twt"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk05", description: "This is some text", title: "Not again...", image: <PFFile: 0x600000240cc0>, id: "JNqaAgtdRb"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk06", description: "This is some text", title: "Foreign cars", image: <PFFile: 0x600000240f30>, id: "Hb16TDXGbz"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk07", description: "This is some text", title: "Skin problems", image: <PFFile: 0x6000002411a0>, id: "MUYDMnJCrU"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk08", description: "This is some text", title: "Junk food", image: <PFFile: 0x600000241410>, id: "yowAfJlcmr"), static ParseStarterProject_Swift.BuildArray.(buildArrayFromQuery (queryForCollection : Swift.String, sender : __ObjC.UIViewController, completeBlock : (Any) -> ()) -> ()).(collectionStruct #1)(name: "pk09", description: "This is some text", title: "Delusion", image: <PFFile: 0x600000241680>, id: "TiLAKBPmaD")]]
previously i was using:
cell.labelCell.text = self.packArray[indexPath.item].packDescription
but this now returns that there is no member packDescription. I probably could go and use string handling and create a mess but there must be a better way of getting the data as an actual array. i know I'm casting it as Any but couldn't figure out another way to get the data from a struct array through a completion block.
----- EDIT ----
In the class where the array is created there is a struct, and array from struct the result is passed through the completeBlock as Any
class func buildArrayFromQuery(queryForCollection: String,
sender: UIViewController,
completeBlock: @escaping (_ result: Any) -> Void) {
struct collectionStruct {
var name : String
var description : String
var title : String
var image : PFFile
var id: String
}
var collectionArray = [collectionStruct]()
this is then created from a PFQuery, and the resultant array is passes back through the completion handler
query.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error!)
} else if let packs = objects {
for object in packs {
print(fromName)
print(object)
let arrayName = object.object(forKey: fromName) as! String
let arrayDescription = object.object(forKey: fromDescription) as! String
let arrayTitle = object.object(forKey: fromTitle) as! String
let arrayImage = object.object(forKey: fromImage) as! PFFile
let arrayID = object.objectId as String!
collectionArray.append(collectionStruct(name: arrayName, description: arrayDescription, title: arrayTitle, image: arrayImage, id: arrayID!))
}
}
completeBlock(result: collectionArray)
})
You need to cast the result of the query to the desired type. If you make packArray an optional, you could assign it directly using
var packArray: [collectionStruct]?
self.packArray = result as? [collectionStruct]
With that code, if result can't be cast to the correct array type, self.packArray will contain nil.
Or you could use optional binding:
var packArray: [collectionStruct] = []
...
if structsArray = result as [collectionStruct] {
self.packArray = structsArray
}
You should define packArray outside of viewDidLoad:
var packArray: [collectionStruct] = []
override func viewDidLoad() {
super.viewDidLoad()
BuildArray.buildArrayFromQuery(queryForCollection: "Pack",
sender: self) {
(result) in
if structsArray = result as [collectionStruct] {
self.packArray = structsArray
}
}
Your code defines a local variable packArray inside viewDidLoad, which confuses things. I advise against using an instance variable and a local variable with the same name.
这篇关于Swift-通过块传递结构数组并检索成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!