2016-09-02 43 views
1

中更新我的xcode以獲得最新的測試版,並且出現了這樣的問題。 之前我的代碼在Objective-C運行良好:如何在一個快速的3

short * pointers = (short *)[[params valueForKey:@"Pointers"] pointerValue]; 

然後我翻譯的迅速語言代碼的一部分:

let pointers = UnsafeMutablePointer<Int16>(((params["Pointers"] as AnyObject).pointerValue)!) 

在這裏,一切都很好。 But this code is not working on the Swift 3。告訴你如何快速實施3.謝謝你!

+0

嘗試鑄造NSValue, '(params [「Pointers」]作爲NSValue).pointerValue' –

+0

@LeoDabus感謝您的答案。錯誤: 「不能調用初始化類型 'UnsafeMutablePointer ' 有型 '(UnsafeMutableRawPointer)' – Feanon

+1

參數列表@LeoDabus讓數據A = unsafeBitCast((PARAMS [」 指針「]作爲NSValue).pointerValue,到:UnsafeMutablePointer .self)。工作!:)。謝謝你的幫助 – Feanon

回答

1

轉換指針的UInt16正確的方法,其被包裝在一個NSValueUnsafeMutablePointer<Int16>斯威夫特卡倫特3

if let ptr = value.pointerValue { 
    let u16ptr = ptr.assumingMemoryBound(to: UInt16.self) 
    // ... 
} else { 
    // pointer is nil 
} 

你的情況應該是

if let ptr = (params["Pointers"] as? NSValue)?.pointerValue { 
    let u16ptr = ptr.assumingMemoryBound(to: UInt16.self) 
    // ... 
} else { 
    // value does not exist, is not an `NSValue`, or pointer is nil 
} 
+0

我用你的代碼! – Feanon