小编Den*_*nis的帖子

在Swift 1.2中将self作为参数传递给init方法

以下类具有let声明为隐式展开变量的' '属性.以前这适用于Xcode 6.2:

class SubView: UIView {

    let pandGestureRecognizer: UIPanGestureRecognizer!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.pandGestureRecognizer = UIPanGestureRecognizer(target: self, action: "panAction:")
    }

    func panAction(gesture: UIPanGestureRecognizer) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

更新到Xcode 6.3(使用Swift 1.2)后,会发生以下编译错误:

Property 'self.panGestureRecognizer' not initialized at super.init call
Immutable value 'self.panGestureRecognizer' may only be initialized once
Run Code Online (Sandbox Code Playgroud)

super.init通话前移动以下行:

self.pandGestureRecognizer = UIPanGestureRecognizer(target: self, action: "panAction:")
Run Code Online (Sandbox Code Playgroud)

给出以下错误:

'self' is used before super.init call
Run Code Online (Sandbox Code Playgroud)

该属性' panGestureRecognizer'不需要变异,因此必须将其声明为常量' let'.由于它是常量,因此必须在声明时具有初始值,或者在init方法中初始化它.要初始化它,需要self在' target'参数中传递' ' …

xcode ios swift

9
推荐指数
1
解决办法
5750
查看次数

将[Int64]的Swift数组转换为NSArray

我想斯威夫特的数组转换Int64NSArrayNSNumber价值观.

@interface A : NSObject
- (void)bar:(NSArray *)tips;
@end
Run Code Online (Sandbox Code Playgroud)

Swift类继承了这个Objective-C类:

class B : A {
    func foo(tips : [Int64]) {
        self.bar(tips)
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift代码无法编译时出现以下错误:

Type '[Int64]' does not conform to protocol 'AnyObject'
Run Code Online (Sandbox Code Playgroud)

我怎么能转换[Int64]NSArrayNSNumber实例?

PS我尝试了很多东西,找不到一个简单的方法来做到这一点:

self.bar(NSArray(array: tips))
self.bar(tips as NSArray)
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题涉及不涉及试图建立新NSArray的独立的Int64对象,但现有的数组转换[Int64]NSArray

arrays interop objective-c ios swift

5
推荐指数
1
解决办法
3196
查看次数

标签 统计

ios ×2

swift ×2

arrays ×1

interop ×1

objective-c ×1

xcode ×1