2019-04-29 11:46:48 -04:00
|
|
|
package main
|
2019-06-11 12:38:22 -04:00
|
|
|
|
2019-04-30 09:14:25 -04:00
|
|
|
//go:generate nswrap
|
2019-04-29 11:46:48 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-27 11:41:58 -04:00
|
|
|
"os"
|
2019-04-29 11:46:48 -04:00
|
|
|
|
2019-05-29 12:57:53 -04:00
|
|
|
"git.wow.st/gmp/nswrap/examples/foundation/ns"
|
2019-04-29 11:46:48 -04:00
|
|
|
)
|
|
|
|
|
2019-06-13 16:47:36 -04:00
|
|
|
var (
|
|
|
|
gs1 *ns.NSString
|
|
|
|
gs2 *ns.NSString
|
|
|
|
)
|
|
|
|
|
2019-04-29 11:46:48 -04:00
|
|
|
func main() {
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("Creating some strings:\n")
|
2019-05-03 13:14:30 -04:00
|
|
|
n1 := ns.NSStringWithUTF8String(ns.CharWithGoString("hi there"))
|
2019-04-29 11:46:48 -04:00
|
|
|
c1 := n1.CapitalizedString()
|
|
|
|
gs := c1.UTF8String().String()
|
|
|
|
fmt.Println(gs)
|
2019-05-06 13:21:36 -04:00
|
|
|
n2 := ns.NSStringAlloc()
|
|
|
|
n2 = n2.InitWithGoString("hi world")
|
2019-05-03 13:14:30 -04:00
|
|
|
n3 := ns.NSStringWithGoString("ok bye")
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("%s\n", n3)
|
2019-05-28 15:52:16 -04:00
|
|
|
|
|
|
|
fmt.Printf("\nCreating an array:\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
a := ns.NSMutableArrayWithObjects(n1, n2, n3)
|
|
|
|
fmt.Println("Length(a) = ", a.Count())
|
|
|
|
fmt.Println("is n2 in a?", a.ContainsObject(n2))
|
|
|
|
fmt.Println("is c1 in a?", a.ContainsObject(c1))
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nCreating substrings and adding to the array:\n")
|
2019-05-01 10:58:29 -04:00
|
|
|
n4 := n2.SubstringFromIndex(3)
|
|
|
|
n5 := n3.SubstringToIndex(4)
|
|
|
|
a.AddObject(n4)
|
|
|
|
a.AddObject(n5)
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Println("Length(a) = ", a.Count())
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nSubarray with range:\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
a2 := a.SubarrayWithRange(ns.NSMakeRange(1, 3))
|
|
|
|
fmt.Println("Length(a2) = ", a2.Count())
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nObjectAtIndex(1):\n")
|
2019-05-01 12:32:42 -04:00
|
|
|
i1 := a.ObjectAtIndex(1).NSString()
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("i1 = %@\n", i1)
|
|
|
|
fmt.Printf("i1.Ptr() = %p\n", i1.Ptr())
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nNSArray.ObjectEnumerator().ForIn():\n")
|
2019-06-27 11:41:58 -04:00
|
|
|
x := 0
|
2019-06-13 09:25:53 -04:00
|
|
|
a.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
2019-06-27 11:41:58 -04:00
|
|
|
fmt.Printf("%d: %s\n",x,o.NSString())
|
|
|
|
x++
|
2019-05-02 14:14:29 -04:00
|
|
|
return true
|
|
|
|
})
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nNSSetWithObjectsCount():\n")
|
2019-06-13 09:25:53 -04:00
|
|
|
s1 := ns.NSSetWithObjectsCount(&[]*ns.Id{&n1.Id, &n2.Id}, 2)
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nNSSet.ObjectEnumerator().ForIn():\n")
|
2019-06-13 09:25:53 -04:00
|
|
|
s1.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Println(o.NSString())
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
fmt.Printf("\nNSMutableArrayWithObjects()\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
a = ns.NSMutableArrayWithObjects(n1, s1)
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nNSArray.ObjectEnumerator().ForIn():\n")
|
2019-06-13 09:25:53 -04:00
|
|
|
a.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("%s -- ", o.ClassName().UTF8String())
|
2019-05-23 16:31:47 -04:00
|
|
|
switch {
|
|
|
|
case o.IsKindOfClass(ns.NSStringClass()):
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf(" It's a string\n")
|
2019-05-23 16:31:47 -04:00
|
|
|
case o.IsKindOfClass(ns.NSSetClass()):
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf(" It's a set\n")
|
2019-05-23 16:31:47 -04:00
|
|
|
default:
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf(" I don't know what it is!\n")
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-06-27 11:41:58 -04:00
|
|
|
fmt.Printf("a = %p a.NSArray = %p\n",a,&a.NSArray)
|
2019-06-13 16:47:36 -04:00
|
|
|
fmt.Printf("\nNSArrayWithObjects() (length 1)\n")
|
|
|
|
a2 = ns.NSArrayWithObjects(n1)
|
|
|
|
a2.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
|
|
|
switch {
|
|
|
|
case o.IsKindOfClass(ns.NSStringClass()):
|
|
|
|
fmt.Println(o.NSString().UTF8String())
|
|
|
|
return true // continue enumeration
|
|
|
|
default:
|
|
|
|
fmt.Println("Unknown class")
|
|
|
|
return false // terminate enumeration
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nNSArrayWithObjects()\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
a2 = ns.NSArrayWithObjects(n1, n2, n3, s1)
|
2019-05-28 15:52:16 -04:00
|
|
|
fmt.Printf("\nNSArray.ObjectEnumerator().ForIn():\n")
|
2019-06-13 09:25:53 -04:00
|
|
|
a2.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
2019-05-23 16:31:47 -04:00
|
|
|
switch {
|
|
|
|
case o.IsKindOfClass(ns.NSStringClass()):
|
|
|
|
fmt.Println(o.NSString().UTF8String())
|
2019-06-11 12:38:22 -04:00
|
|
|
return true // continue enumeration
|
2019-05-23 16:31:47 -04:00
|
|
|
default:
|
|
|
|
fmt.Println("Unknown class")
|
2019-06-11 12:38:22 -04:00
|
|
|
return false // terminate enumeration
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
|
|
|
})
|
2019-05-28 15:52:16 -04:00
|
|
|
|
|
|
|
nst := ns.NSStringWithGoString
|
2019-05-28 20:04:39 -04:00
|
|
|
fmt.Printf("\nNSDictionaryWithObjectsForKeys()\n")
|
2019-05-28 15:52:16 -04:00
|
|
|
d := ns.NSDictionaryWithObjectsForKeys(
|
2019-06-11 12:38:22 -04:00
|
|
|
ns.NSArrayWithObjects(nst("obj1"), nst("obj2")),
|
|
|
|
ns.NSArrayWithObjects(nst("key1"), nst("key2")),
|
2019-05-28 15:52:16 -04:00
|
|
|
)
|
2019-06-27 11:41:58 -04:00
|
|
|
oarr := make([]*ns.Id, 0, 5)
|
|
|
|
fmt.Printf("Length of oarr is %d\n", len(oarr))
|
|
|
|
karr := make([]*ns.Id, 0, 5)
|
2019-05-28 20:04:39 -04:00
|
|
|
fmt.Printf("\nGetObjects()\n")
|
2019-06-27 11:41:58 -04:00
|
|
|
d.GetObjects(&oarr, &karr, 4)
|
|
|
|
fmt.Printf("Length of oarr is now %d\n", len(oarr))
|
|
|
|
for i, k := range karr {
|
|
|
|
fmt.Printf("-- %s -> %s\n", k.NSString(), oarr[i].NSString())
|
2019-05-28 15:52:16 -04:00
|
|
|
}
|
2019-05-28 20:04:39 -04:00
|
|
|
fmt.Printf("\nNSStringWithContentsOfURLEncoding()\n")
|
2019-06-13 09:25:53 -04:00
|
|
|
err := make([]*ns.NSError, 1)
|
2019-06-27 11:41:58 -04:00
|
|
|
n1 = ns.NSStringWithContentsOfURLEncoding(ns.NSURLWithGoString("http://captive.apple.com"), ns.NSUTF8StringEncoding, &err)
|
|
|
|
if len(err) == 0 {
|
|
|
|
fmt.Printf("n1 = %s\n",n1)
|
|
|
|
}
|
|
|
|
n1 = ns.NSStringWithContentsOfURLEncoding(ns.NSURLWithGoString("htttypo://example.com"), ns.NSUTF8StringEncoding, &err)
|
|
|
|
if len(err) > 0 {
|
|
|
|
fmt.Printf("err[0] = %p -> %p\n",err[0],err[0].Ptr())
|
|
|
|
fmt.Printf("err: %s\n", err[0].LocalizedDescription())
|
|
|
|
}
|
2019-05-28 20:04:39 -04:00
|
|
|
|
|
|
|
fmt.Printf("\nNSStringWithFormat()\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
str := ns.NSStringWithFormat(nst("(%@) (%@)\n(%@)\n"), n2, n3, s1)
|
|
|
|
fmt.Printf("%s\n", str)
|
2019-06-13 16:47:36 -04:00
|
|
|
|
|
|
|
fmt.Printf("\nGlobal strings\n")
|
|
|
|
gs1 = ns.NSStringWithGoString("global string 1")
|
|
|
|
gs2 = ns.NSStringWithGoString("global string 2")
|
|
|
|
fmt.Printf("\nArrayWithObjects\n")
|
|
|
|
a2 = ns.NSArrayWithObjects(gs1, gs2)
|
|
|
|
a2.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
|
|
|
fmt.Printf("--%s\n",o.NSString())
|
|
|
|
return true
|
|
|
|
})
|
2019-06-27 11:41:58 -04:00
|
|
|
dir,e := os.Getwd()
|
|
|
|
if e != nil {
|
|
|
|
fmt.Printf("Failed to get current working directory. %s\n",err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
path := nst(dir)
|
|
|
|
filter := ns.NSArrayWithObjects(nst("ast"),nst("yaml"))
|
|
|
|
ost := make([]*ns.NSString,0,1)
|
|
|
|
oar:= make([]*ns.NSArray,0,1)
|
|
|
|
fmt.Printf("\nCompletePathIntoString()\n")
|
|
|
|
i := path.CompletePathIntoString(&ost,0,&oar,filter)
|
|
|
|
fmt.Printf("%d matches\n",i)
|
|
|
|
if i > 0 {
|
|
|
|
fmt.Printf("ost = %s\n",ost[0])
|
|
|
|
fmt.Printf("oar =\n")
|
|
|
|
oar[0].ObjectEnumerator().ForIn(func(o *ns.Id) bool {
|
|
|
|
fmt.Printf("--%s\n",o.NSString())
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2019-04-29 11:46:48 -04:00
|
|
|
}
|