Arrays 转换类型为'的值;结果?';输入';字符串';

Arrays 转换类型为'的值;结果?';输入';字符串';,arrays,swift,casting,Arrays,Swift,Casting,我无法将“result”类型的数组强制转换为字符串数组。这是我已经尝试过的: 编辑: 我需要字符串类型的信息,因为我想使用URL作为图像源 swift4 let message = try? JSONDecoder().decode(Welcome.self, from: data) let imageURLs = message?.children.attachment.results.filter({ $0.metadata.mediaType == "image/png" }) let l

我无法将“result”类型的数组强制转换为字符串数组。这是我已经尝试过的:

编辑: 我需要字符串类型的信息,因为我想使用URL作为图像源

swift4

let message = try? JSONDecoder().decode(Welcome.self, from: data)
let imageURLs = message?.children.attachment.results.filter({ $0.metadata.mediaType == "image/png" })
let latestImageURls = imageURLs?.prefix(2)
let latestImageURlsArray = Array(latestImageURls ?? [])

let image1 = self.view.viewWithTag(63) as! UIImageView
let image2 = self.view.viewWithTag(64) as! UIImageView
let image3 = self.view.viewWithTag(65) as! UIImageView

let url1 = URL(string: latestImageURlsArray[0]) // error:Cannot convert value of type 'Result' to expected argument type 'String
let url2 = URL(string: latestImageURlsArray[1]) // error:Cannot convert value of type 'Result' to expected argument type 'String
let url3 = URL(string: latestImageURlsArray[2]) // error:Cannot convert value of type 'Result' to expected argument type 'String

image1.kf.setImage(with: url1)
image2.kf.setImage(with: url2)
image3.kf.setImage(with: url3)
  • 你没有做任何“铸造”,你只是在说“我期待一个
    [Array]
    ”。你没有做任何事情来证明这一点。在任何情况下,这都是无效的,因为
    Array
    不是有效的类型
  • 如您所见,
    prefix
    返回一个
    ArraySlice
    ,它在
    数组
    的内存中提供了一个轻量级视图,而无需复制任何元素

  • 我想根本就没有所谓的
    [Array]
    ,我猜你说的是
    数组
    结果
    对象的数组。与
    [Result]
    相同的是什么

    如果出于某种原因,您想从
    ArraySlice
    创建一个新的
    Array
    对象,只需调用初始值设定项即可

    let resultsArray = Array(latestImageURls ?? [])
    
    更新

    您的意思是您还需要将
    结果
    对象转换为
    字符串
    ,但您没有解释什么是
    结果
    对象以及它与
    字符串
    的关系。它是包含它还是是一个字符串?所以我试着假设

    如果你想将对象转换成字符串,你可以这样做

    let latestImageURlsArray = resultsArray.compactMap { $0 as? String }
    
    let latestImageURlsArray = resultsArray.compactMap { $0.imageURL }
    
    如果您想从结果中提取字符串(假设它存储在
    imageURL
    参数中),您可以这样做

    let latestImageURlsArray = resultsArray.compactMap { $0 as? String }
    
    let latestImageURlsArray = resultsArray.compactMap { $0.imageURL }
    
    之后,
    latestImageURlsArray
    将成为一个
    [String]


    如果关系完全不同且更复杂,请添加有关
    结果
    类的更多详细信息,以便我可以使我的答案更具体。

    通常,将给定类型的
    阵列片
    转换为该类型的
    阵列
    的方法是使用数组初始值设定项:

    struct Thing {
    }
    
    let things = [Thing]()
    let sliceOfArrayOfThings = things.prefix(2)
    let arrayOfThings = Array(sliceOfArrayOfThings)
    
    在您的例子中,您的数组实际上是一个可选数组,因此您必须采取一些额外的步骤来处理可选性,如Yury在其回答中所述:

    let things: [Thing]? = []
    let sliceOfArrayOfThings = things?.prefix(2)
    let arrayOfThings = Array(sliceOfArrayOfThings ?? [])
    

    您需要一个
    数组来做什么?什么是
    ArraySlice
    不够用?
    ArraySlice
    不够用optional@Carpsen90当然,你是对的。我已经更新了我的答案。@Yurymashev:是的,回答很好,但我仍然无法转换类型。现在我遇到了从“结果”转换为“字符串”的问题。我需要字符串作为我要显示的图片的源。->我更新了我的帖子,“因为数组不是有效的类型”。请注意,
    Array
    在数组实例属性或方法扩展中用作返回类型时可能是有效的类型,它与
    [Element]
    类型等效inference@LeoDabus是的,我知道,但根据概率的平衡,它可能不是在这样的背景下。