我想從我的一個視圖控制器讀取一個字節數組到另一個,請在下面找到我的代碼。如何在swift中正確讀取外設值?
從我的第一個視圖
class First: UIViewController {
var myByteArray = [UInt8](repeating:0, count: 20)
viewDidLoad(){
......}
幾條語句後她在那裏我在一個函數讀我的數據
func passThis(){
let ReceiveData = rxCharacteristic?.value
if let ReceiveData = ReceiveData {
let ReceivedNoOfBytes = ReceiveData.count
myByteArray = [UInt8](repeating: 0, count: ReceivedNoOfBytes)
(ReceiveData as NSData).getBytes(&myByteArray, length: ReceivedNoOfBytes)
print("Data Received ",myByteArray)
}
這是我的,我試圖來讀取首先我陣列的第二視圖查看
class Second: UIViewController {
var myByteArray2 = [UInt8](repeating: 0, count: 20)
viewDidLoad(){
super.viewDidLoad()
let fvc = First()
myByteArray2 = fvc.myByteArray
print(myByteArray2)
}
現在我有[11,12,13,14,15,16,17,18,19,20]從myByteArray 但myByteArray2中有[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]?
有人可以幫忙嗎?
另外,如何在寫入和讀取新值之前清除rxCharacterstic中的readValue緩衝區?
任何幫助/意見表示讚賞。
感謝
編輯 - >我的傳球是如何做
從BLECentral
class BLECentral: ...
var centralManager: CBCentralManager!
//After Scanning and connecting
func centralManager(_central: CBCentralManager, didConnect peripheral: CBPeripheral){
peripheral.delegate = self
peripheral.discoverServices([BLEUUID])
//Once connected, move to new view controller to manager incoming and outgoing data
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC = storyboard.instantiateViewController(withIdentifier: "First") as! First
firstVC.peripheral = peripheral
navigationController?.pushViewController(firstVC, animated: true)
}
現在在我的第一個下準備賽格瑞塊我傳遞周圍像這樣
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is Second
{
let vc2 = segue.destination as? Second
vc2.periperal = blePeripheral
}
}
你是如何轉換視圖控制器?首先呼叫第二? – Diesel
@柴油嗨柴油,非常感謝,我能夠解決它與您的答案一點點閱讀:) – nar0909
沒問題。你在做什麼是正確的!我建議閱讀Apple教程(只需閱讀它),因爲它可以節省您的時間並涵蓋基本知識。 https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/是一個很好的開始或免費的書籍,如果你有更多的時間在名爲「應用程序開發與斯威夫特」的書店。 – Diesel