本文将带您了解关于在Swift2中重写func错误的新内容,同时我们还将为您解释swift重写init方法的相关知识,另外,我们还将为您提供关于.toInt()在Swift2中删除?、function
本文将带您了解关于在Swift 2中重写func错误的新内容,同时我们还将为您解释swift重写init方法的相关知识,另外,我们还将为您提供关于.toInt()在Swift 2中删除?、function – 在Swift 2.0中使用reduce()时出错、Init已在Swift 3中重命名为init(描述)错误、ios – 从swift 1.2迁移后swift2中的healthKit错误的实用信息。
本文目录一览:- 在Swift 2中重写func错误(swift重写init方法)
- .toInt()在Swift 2中删除?
- function – 在Swift 2.0中使用reduce()时出错
- Init已在Swift 3中重命名为init(描述)错误
- ios – 从swift 1.2迁移后swift2中的healthKit错误
在Swift 2中重写func错误(swift重写init方法)
XCode 6中的此代码没有错误,但是在XCode 7(Swift 2)中发生了此错误:
方法不会覆盖其超类中的任何方法
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { /* Called when a touch begins */}
删除override
单词时,发生此错误:
方法’的touchesBegan( ::)
withEvent’与目标C选择’的touchesBegan:withEvent:方法’与方法冲突’的touchesBegan(
:从超类的UIResponder’具有相同的目标C选择withEvent :)’
答案1
小编典典您遇到了第一个错误,因为许多Cocoa Touch已通过审核以支持Objective-
C泛型,这意味着可以键入数组和集合之类的元素。因此,此方法的签名已更改,并且由于您编写的内容不再与之匹配,因此会出现错误,说明您已将方法标记为,override
但实际上与以下任何方法都不匹配超级班。
然后,当您删除override
关键字时,您得到的错误是让您知道您使用真正的touched begin方法制作了一个与Objective-
C选择器冲突的方法(与Swift不同,Objective-C不支持method超载)。
最重要的是,在Swift 2中,您的触摸开始覆盖应该看起来像这样。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { // stuff}
有关Objective-
C泛型对您的Swift代码意味着什么的更多信息,建议您查看使用Swift和Cocoa和Objective-C的预发行版中的“轻量级泛型”部分。截至第33和34页。
.toInt()在Swift 2中删除?
textField.text.toInt()
工作。现在Swift声明这是一个错误,并告诉我做
textField.text!.toInt()
并且它说没有toInt()和尝试使用Int()。这也不工作。刚刚发生了什么?
Int(myString)
在你的情况下,你可以使用int(textField.text!)insted的textField.text!.toInt()
Swift 1.x
let myString: String = "256" let myInt: Int? = myString.toInt()
Swift 2.x,3.x
let myString: String = "256" let myInt: Int? = Int(myString)
function – 在Swift 2.0中使用reduce()时出错
当我尝试使用reduce函数时,我收到一条错误消息:
reduce is unavailable: call the ‘reduce()’ method on the sequence
我已经知道如何使用enumerate()函数执行此操作,但我似乎无法解决此问题.这是返回错误的代码行:
var hashValue: Int { return reduce(blocks,0) { $0.hashValue ^ $1.hashValue } }
var hashValue: Int { return blocks.reduce(0) { $0.hashValue ^ $1.hashValue } }
Init已在Swift 3中重命名为init(描述)错误
guard let userData = responseData["UserProfile"] as? [String : AnyObject] else { return } var userProfileFieldsDict = [String: String]() if let profileUsername = userData["Username"] as? Nsstring { userProfileFieldsDict["username"] = String(profileUsername) } if let profileReputationpoints = userData["ReputationPoints"] as? NSNumber { userProfileFieldsDict["reputation"] = String(profileReputationpoints) }
但是,在Swift 3中,它会在userProfileFieldsDict [“reputation”]上引发错误
init has been renamed to init(describing:)
我的问题是为什么它会触发该行而不是userProfileFieldsDict [“username”]赋值行,以及如何修复它?我假设它是因为我正在将一个NSNumber转换为String,但我无法理解为什么这很重要.
而是使用String(describe :)构造函数,如下所示
userProfileFieldsDict["reputation"] = String(describing: profileReputationpoints)
关于它,这里有更多info.
ios – 从swift 1.2迁移后swift2中的healthKit错误
…’_’不能转换为’HKWorkoutType’
出现错误的代码行:
let healthKitTypestoWrite = Set(arrayLiteral:[ HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned),HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierdistanceWalkingRunning),HKQuantityType.workoutType() ])
有任何想法吗?
解决方法
let healthKitTypestoWrite = Set(arrayLiteral: [ HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)!,HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierdistanceWalkingRunning)!,HKQuantityType.workoutType() ])
这是必需的,因为quantityTypeForIdentifier返回HKQuantityType?
我们今天的关于在Swift 2中重写func错误和swift重写init方法的分享已经告一段落,感谢您的关注,如果您想了解更多关于.toInt()在Swift 2中删除?、function – 在Swift 2.0中使用reduce()时出错、Init已在Swift 3中重命名为init(描述)错误、ios – 从swift 1.2迁移后swift2中的healthKit错误的相关信息,请在本站查询。
本文标签: