NSData类型的自定义类未命中-bytes

时间:2018-11-01 14:25:17

标签: ios swift bluetooth nsdata

我目前正在使用iOS应用程序(用Swift编写),该应用程序应连接两个(以后)的iOS设备,并使用蓝牙在它们之间传输数据。实际使用应该是音乐/音频分发,但是现在我在发送简单文本消息时遇到了很多问题。

我使用Core Bluetooth Framework(低功耗蓝牙4.0)设置外围设备和中央设备的角色,并且(成功建立连接后)我使用此外围设备功能将数据存储在适当的特征中,以供中央设备读取:

public class TextBoxAppender : AppenderSkeleton
{
    private RichTextBox _textBox;
    public string FormName { get; set; }
    public string TextBoxName { get; set; }


    public static Control FindContrl(Control root, String searchName)
    {
        foreach (Control control in root.Controls)
        {

            if (control.Name.Equals(searchName))
            {
                return control;
            }


            if (control.Controls != null)
            {
                Control c = FindContrl(control, searchName);
                if (c != null) return c;
            }
        }
        return null;
    }


    public delegate void doLog( String value , RichTextBox lger ) ;

    public void doLg(String value, RichTextBox lger)
    {
        lger.AppendText(value);
    }


    protected override void Append(LoggingEvent loggingEvent)
    {
        if (_textBox == null)
        {
            if (String.IsNullOrEmpty(FormName) ||
                String.IsNullOrEmpty(TextBoxName))
                return;

            Form form = Application.OpenForms[FormName];
            if (form == null)
                return;



            Control c = FindContrl(form, "logTextBox"); 
            if (c != null)
            {
                _textBox = (RichTextBox)c;
            }



            if (_textBox == null)
                return;

            form.FormClosing += (s, e) => _textBox = null;
        }

        doLog a = new doLog(doLg);

        if (_textBox.InvokeRequired)
        {
            _textBox.Invoke(a, new Object[] { loggingEvent.RenderedMessage + Environment.NewLine, _textBox });

        }
        else
        {
            _textBox.AppendText(loggingEvent.RenderedMessage + Environment.NewLine);
        }


    }
}

我想以自定义数据类型“ ChatMessage” 传输数据,该数据类型包含一个用于存储消息文本的String变量和一个用于存储发送设备名称的String。该类是从 NSData 类型派生的,因此我还实现了必需的init / encode / decode函数。

我认为将我的 NSData 类转换为上述功能所需的 Data 类型没有问题。但是在运行时我会收到此错误:

func updateValue(Data, for: CBMutableCharacteristic, onSubscribedCentrals: [CBCentral]?) -> Bool

在自定义类中是否还必须实现一个(getter?-)变量或函数?如果是,那应该是什么样子?我在iOS开发中是一个新手,所以我不知道该类的字节是做什么用的。

谢谢!

我正在使用XCode版本10.0,Swift 4,并用于测试带有iOS 12.0.1的iPhone XS Max和带有iOS 12.0的iPad Air 2。

0 个答案:

没有答案
相关问题