无法将类型的值“米姆!”预期参数类型'@noescape(米姆)抛出 - >布尔“ [英] Cannot convert value of type 'Meme!' to expected argument type '@noescape (Meme) throws -> Bool'
问题描述
下面是code:
@IBAction FUNC deleteMeme(发件人:的UIBarButtonItem){
如果让foundIndex = MemeRepository.sharedInstance.memes.indexOf(selectedMeme){
//发现的索引中删除项目
MemeRepository.sharedInstance.memes.removeAtIndex(foundIndex)
navigationController?.popViewControllerAnimated(真)
错误的 .indexOf
方法发生在(selectedMeme)
。
无法转换类型的值米姆
来预期的参数类型 @noescape(米姆)抛出 - >布尔
米姆!是我的应用程序的结构体。我怎么通过这个工作吗?
结构米姆{VAR的TopText:串!
VAR bottomText:串!
VAR图像:UIImage的!
VAR memedImage:UIImage的!的init(的TopText:字符串,bottomText:字符串,图像:UIImage的,memedImage:UIImage的){
self.topText =的TopText
self.bottomText = bottomText
self.image =图像
self.memedImage = memedImage
错误消息是误导。你真正需要的是提供编译器的方式来比较两个米姆
实例,并决定在其标准这些实例都是平等的。
假设你想要具有相同名称
属性两个实例被视为平等的。
我们使结构符合 Equatable
我们也创建一个 ==
操作员通过他们的两个结构比较name属性:
结构米姆:Equatable {
变数名称:串!
}FUNC ==(左:米姆,RHS:梅梅) - GT;布尔{
返回lhs.name == rhs.name
}
现在我们可以使用的indexOf
与米姆的实例:
让总督=米姆(名称:总督)让lolcat =米姆(名称:lolcat)让模因= [lolcat,总督]如果让dogeIndex = memes.indexOf(公爵){
打印(dogeIndex)// 1
}
如果您希望通过他们的名称
属性,但其独特性,以两个实例比不上,那么你将不得不作出的结构符合哈希
并使用独特的散列值
属性返回一个诠释:
结构米姆:哈希{
变数名称:串!
VAR散列值:诠释{
返回self.name.hashValue
}
的init(名称:字符串){
self.name =名称
}
}FUNC ==(左:米姆,RHS:梅梅) - GT;布尔{
返回lhs.hashValue == rhs.hashValue
}让公爵=米姆(名称:总督)让lolcat =米姆(名称:lolcat)让模因= [lolcat,总督]如果让dogeIndex = memes.indexOf(公爵){
打印(dogeIndex)// 1
}
在这个例子中,散列值
是 self.name
制作,因此两个不同的米姆的实例与同一name属性将被视为相等。如果你不希望出现这种情况,请使用其他来源的哈希值。
Here is the code:
@IBAction func deleteMeme(sender: UIBarButtonItem) {
if let foundIndex = MemeRepository.sharedInstance.memes.indexOf(selectedMeme) {
//remove the item at the found index
MemeRepository.sharedInstance.memes.removeAtIndex(foundIndex)
navigationController?.popViewControllerAnimated(true)
The error happens at the .indexOf
method at (selectedMeme)
.
Cannot convert value of type Meme!
to expected argument type @noescape (Meme) throws -> Bool
Meme! is a struct for my app. How do I work through this?
struct Meme {
var topText : String!
var bottomText: String!
var image: UIImage!
var memedImage: UIImage!
init(topText: String, bottomText: String, image: UIImage, memedImage: UIImage) {
self.topText = topText
self.bottomText = bottomText
self.image = image
self.memedImage = memedImage
The error message is misleading. What you actually need is to provide the compiler a way to compare two Meme
instances and decide upon which criteria those instances are equal.
Let's say you want two instances having the same name
property to be treated as equal.
We make the struct conform to Equatable
and we also create an ==
operator that compares two structs by their name property:
struct Meme:Equatable {
var name:String!
}
func ==(lhs: Meme, rhs: Meme) -> Bool {
return lhs.name == rhs.name
}
Now we can use indexOf
with a Meme instance:
let doge = Meme(name: "doge")
let lolcat = Meme(name: "lolcat")
let memes = [lolcat, doge]
if let dogeIndex = memes.indexOf(doge) {
print(dogeIndex) // 1
}
If you wanted to compare two instances not by their name
property but by their uniqueness, then you would have to make the struct conform to Hashable
and use a unique hashValue
property returning an Int:
struct Meme:Hashable {
var name:String!
var hashValue: Int {
return self.name.hashValue
}
init(name: String) {
self.name = name
}
}
func ==(lhs: Meme, rhs: Meme) -> Bool {
return lhs.hashValue == rhs.hashValue
}
let doge = Meme(name: "doge")
let lolcat = Meme(name: "lolcat")
let memes = [lolcat, doge]
if let dogeIndex = memes.indexOf(doge) {
print(dogeIndex) // 1
}
In this example the hashValue
is made from self.name
, so two different instances of Meme with a same name property will be considered equal. If you don't want that, use another source for the hash value.
这篇关于无法将类型的值“米姆!”预期参数类型'@noescape(米姆)抛出 - >布尔“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!