public void Encode() { IntPtr nscoding = Runtime.GetProtocol("NSCoding"); Assert.That(nscoding, Is.Not.EqualTo(IntPtr.Zero), "NSCoding"); #if !XAMCORE_2_0 // NSObject does not conform to NSCoding using (var o = new NSObject()) { Assert.False(o.ConformsToProtocol(nscoding), "NSObject/NSCoding"); using (var c = new NSCoder()) { Assert.Throws <InvalidOperationException> (delegate { o.EncodeTo(c); }, "NSObject/!NSCoding"); } } #endif // NSNumber conforms to NSCoding using (var n = new NSNumber(-1)) { Assert.True(n.ConformsToProtocol(nscoding), "NSNumber/NSCoding"); using (var d = new NSMutableData()) using (var a = new NSKeyedArchiver(d)) { n.EncodeTo(a); a.FinishEncoding(); } } }
public void EncodeDecodeTest() { var buffer = new byte[] { 3, 14, 15 }; var obj = new NSString(); byte[] data; var ptr = Marshal.AllocHGlobal(buffer.Length); for (int i = 0; i < buffer.Length; i++) { Marshal.WriteByte(ptr, i, buffer [i]); } using (var mutableData = new NSMutableData(1024)) { using (var coder = new NSKeyedArchiver(mutableData)) { coder.Encode(obj, "obj"); coder.Encode(buffer, "buffer"); coder.Encode(Int32.MaxValue, "int32"); coder.Encode(float.MaxValue, "float"); coder.Encode(double.MaxValue, "double"); coder.Encode(true, "bool"); coder.Encode(long.MaxValue, "long"); coder.Encode(buffer, 2, 1, "buffer2"); coder.Encode(nint.MaxValue, "nint"); coder.EncodeBlock(ptr, buffer.Length, "block"); coder.FinishEncoding(); } using (var decoder = new NSKeyedUnarchiver(mutableData)) { Assert.IsNotNull(decoder.DecodeObject("obj")); var buf = decoder.DecodeBytes("buffer"); Assert.AreEqual(buf.Length, buffer.Length, "buffer.length"); for (int i = 0; i < buf.Length; i++) { Assert.AreEqual(buf [i], buffer [i], "buffer [" + i.ToString() + "]"); } Assert.AreEqual(Int32.MaxValue, decoder.DecodeInt("int32")); Assert.AreEqual(float.MaxValue, decoder.DecodeFloat("float")); Assert.AreEqual(true, decoder.DecodeBool("bool")); Assert.AreEqual(long.MaxValue, decoder.DecodeLong("long")); buf = decoder.DecodeBytes("buffer2"); Assert.AreEqual(buf.Length, buffer.Length, "buffer2.length"); for (int i = 0; i < buf.Length; i++) { Assert.AreEqual(buf [i], buffer [i], "buffer2 [" + i.ToString() + "]"); } Assert.AreEqual(nint.MaxValue, decoder.DecodeNInt("nint")); buf = decoder.DecodeBytes("block"); Assert.AreEqual(buf.Length, buffer.Length, "block.length"); for (int i = 0; i < buf.Length; i++) { Assert.AreEqual(buf [i], buffer [i], "block [" + i.ToString() + "]"); } } } Marshal.FreeHGlobal(ptr); }
public void sendMessage(NSString msg) { NSDictionary message = NSDictionary.FromObjectAndKey(msg, new NSString("msg")); NSMutableData data = new NSMutableData(); NSKeyedArchiver archiver = new NSKeyedArchiver(data); archiver.Encode(message); archiver.FinishEncoding(); ShowKit.ShowKit.SendMessage(data); }
public void CtorNSCoder () { // NSNumber conforms to NSCoding - so it's .ctor(NSCoder) is usable using (var n = new NSNumber (-1)) using (var d = new NSMutableData ()) { using (var a = new NSKeyedArchiver (d)) { n.EncodeTo (a); a.FinishEncoding (); } using (var u = new NSKeyedUnarchiver (d)) using (var n2 = new NSNumber (u)) { // so we can re-create an instance from it Assert.That (n.Int32Value, Is.EqualTo (-1), "Value"); } } }
public void Encode() { IntPtr nscoding = Runtime.GetProtocol("NSCoding"); Assert.That(nscoding, Is.Not.EqualTo(IntPtr.Zero), "NSCoding"); // NSNumber conforms to NSCoding using (var n = new NSNumber(-1)) { Assert.True(n.ConformsToProtocol(nscoding), "NSNumber/NSCoding"); using (var d = new NSMutableData()) using (var a = new NSKeyedArchiver(d)) { n.EncodeTo(a); a.FinishEncoding(); } } }