2016-03-21 44 views
0

我試圖更新動作的目標值(HMCharacteristicWriteAction),但它總是與EXC_BAD_ACCESS(代碼= 1,地址= 0x50)崩潰。swift - HomeKit updateTarget值HMCharacteristicWriteAction崩潰

我的代碼片段:

print("\(action) --> \(action.dynamicType)") // <HMCharacteristicWriteAction: 0x14cf7ba20> --> HMCharacteristicWriteAction 
print("current: \(action.targetValue)") // current: 1 
print("next: \(value) --> \(value.dynamicType)") // next: 0 --> Int 

action.updateTargetValue(value, completionHandler: { [weak self] error -> Void in 
     if let error = error { 
      // display some message 
      return 
     } 

     // do something else when succeeded 
}) 

正如你看到的,action不是零和正確類型(HMCharacteristicWriteAction)的。我可以成功讀取它的targetValue

我使用Product - Analyze分析了項目,一切正常(沒有任何警告)。我也在Scheme - Diagnostics中啓用了Zombie,但仍然沒有運氣。


根據updateTargetValue文檔:

/*! 
* @brief This method is used to change target value for the characteristic. 
* 
* @param targetValue New target value for the characteristic. 
* 
* @param completion Block that is invoked once the request is processed. 
*     The NSError provides more information on the status of the request, error 
*     will be nil on success. 
*/ 
public func updateTargetValue(targetValue: NSCopying, completionHandler completion: (NSError?) -> Void) 

什麼混淆我是targetValue: NSCopyingvalueInt是否符合'NSCopying'?我曾嘗試value as NSCopyingtargetValue,但它不是更好。

你能告訴我如何解決這個問題嗎?可以通過InttargetValue嗎?什麼可能導致這次崩潰?

謝謝。

回答

0

一個Int不符合NSCopying,使用NSNumber代替它符合NSCopying。

要轉換的詮釋,以NSNumber的使用

[NSNumber numberWithInt:(int)]; 

SWIFT 
    init(int value: Int32) 
OBJECTIVE-C 
- (NSNumber *)initWithInt:(int)value 
+0

喜瑪麗亞,我沒有嘗試的NSNumber但事情沒有得到改善。我的代碼是'let targetValue = NSNumber(integer:value)',然後'actionSet.updateTargetValue(targetValue,...' - >仍然會崩潰,使用EXC_BAD_ACCESS(code = 1,address = 0x50)''。 – quanguyen