Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[NSString] CreateNative, ReleaseNative helper methods that create nat…

…ive strings without creating a full NSString
  • Loading branch information...
commit bba0856218fc6629154674b24d3f9e3450088f33 1 parent d2f76fa
@migueldeicaza migueldeicaza authored
Showing with 27 additions and 1 deletion.
  1. +27 −1 src/Foundation/NSString.cs
View
28 src/Foundation/NSString.cs
@@ -24,7 +24,7 @@
using System.Reflection;
using System.Collections;
using System.Runtime.InteropServices;
-
+using MonoMac.CoreFoundation;
using MonoMac.ObjCRuntime;
namespace MonoMac.Foundation {
@@ -72,12 +72,38 @@ public partial class NSString : NSObject {
static IntPtr selUTF8String = Selector.sel_registerName ("UTF8String");
static IntPtr selInitWithUTF8String = Selector.sel_registerName ("initWithUTF8String:");
static IntPtr selInitWithCharactersLength = Selector.sel_registerName ("initWithCharacters:length:");
+
+#if COREBUILD
+ static IntPtr class_ptr = Class.GetHandle ("NSString");
+#endif
#if GENERATOR && !MONOMAC
public NSString (IntPtr handle) : base (handle) {
}
#endif
+ public static IntPtr CreateNative (string str)
+ {
+ if (str == null)
+ return IntPtr.Zero;
+
+ unsafe {
+ fixed (char *ptrFirstChar = str){
+ var handle = Messaging.intptr_objc_msgSend (class_ptr, Selector.Alloc);
+ handle = Messaging.intptr_objc_msgsend_intptr_int (handle, selInitWithCharactersLength, (IntPtr) ptrFirstChar, str.Length);
+ return handle;
+ }
+ }
+ }
+
+ public static void ReleaseNative (IntPtr handle)
+ {
+ if (handle == IntPtr.Zero)
+ return;
+ Messaging.void_objc_msgSend (handle, Selector.Release);
+ }
+
+
#if false
public NSString (string str) {
if (str == null)
Please sign in to comment.
Something went wrong with that request. Please try again.