检索功能数据 [英] Retrieving Function Data

查看:193
本文介绍了检索功能数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它的数据我真的想检索功能。

I have a function whose data I would really like to retrieve.

在方括号中,我能够打印出值德codedData

Within the brackets, I am able to print out the value DecodedData.

不过,如果我是把打印(德codedData)外面的功能,X code告诉我,'预期的声明,怎么会我能有德codedData 整个文件访问?

However, if I was to put print(DecodedData) just outside the function, Xcode tells me that 'Expected declaration' how would I be able to have DecodedData accessible throughout the file?

我使用的委托方法没有成功尝试,是有没有其他办法?如果是的话,我将如何去这样做呢?

I've tried using the delegate method with no success, is there any other way? and if so, how would I go about doing it?

var DecodedData = ""
//Reading Bluetooth Data
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

    if let data = characteristic.value {
        DecodedData = String(data: data, encoding: NSUTF8StringEncoding)!
    }

    print(DecodedData)
}

我怎么会去有变量德codedData 整座不同斯威夫特文件?

How would I go about having the variable DecodedData available throughout different Swift files?

推荐答案

您可以在类中创建静态变量,并使用它的任何其他文件的快捷

you can create static variable in the class and use it any other swift file.

class YourClass {
static var DecodedData: String = ""
 ...


 func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

   if let data = characteristic.value {
     YourClass.DecodedData = String(data: data, encoding: NSUTF8StringEncoding)!
   }
print(YourClass.DecodedData)
}
}

也可以创建yourclas的单独的对象。

or you can create singleton object of yourclas.

class YourClass {

 static let singletonInstance = YourClass()

 var DecodedData: String = ""

 private init() {
 }

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

if let data = characteristic.value {
  self.DecodedData = String(data: data, encoding: NSUTF8StringEncoding)!
}
}
}

和其他类,你可以通过单独的对象使用。

and in other class you can use by singleton object.

这篇关于检索功能数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆