Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: adding initial user defaults #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions userDefaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gocoa

// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Foundation
// #import "userDefaults.h"
import "C"
import "unsafe"

// UserDefaults - user default settings
type UserDefaults struct {
userDefaultsPtr C.UserDefaultsPtr
}

// NewPUserDefaults - creates a new UserDefaults object
func NewPUserDefaults() *UserDefaults {
defaultsPtr := C.UserDefaults_New()
defaults := &UserDefaults{
userDefaultsPtr: defaultsPtr,
}
return defaults
}

// GetString - reads a string from userDefaults.
func (defaults *UserDefaults) GetString(key string) string {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
value := C.UserDefaults_GetString(defaults.userDefaultsPtr, cKey)
return C.GoString(value)
}

// SetString - writes a string to userDefaults.
func (defaults *UserDefaults) SetString(key string, value string) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
C.UserDefaults_SetString(defaults.userDefaultsPtr, cKey, cValue)
}
10 changes: 10 additions & 0 deletions userDefaults.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import <Foundation/Foundation.h>

// typedef void (*callback)(void);
typedef void* UserDefaultsPtr;

UserDefaultsPtr UserDefaults_New();

void UserDefaults_SetString(UserDefaultsPtr defaultsPtr, const char*key, const char*value);

const char* UserDefaults_GetString(UserDefaultsPtr defaultsPtr, const char*key);
22 changes: 22 additions & 0 deletions userDefaults.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#import "userDefaults.h"
#include "_cgo_export.h"

UserDefaultsPtr UserDefaults_New()
{
id nsUserDefaults = [NSUserDefaults standardUserDefaults];
return (UserDefaultsPtr)nsUserDefaults;
}

void UserDefaults_SetString(UserDefaultsPtr defaultsPtr, const char *key, const char *value)
{
NSUserDefaults* defaults = (NSUserDefaults*)defaultsPtr;
[defaults setObject:[NSString stringWithUTF8String:value] forKey:[NSString stringWithUTF8String:key]];
[defaults synchronize];
}

const char* UserDefaults_GetString(UserDefaultsPtr defaultsPtr, const char *key) {
NSUserDefaults* defaults = (NSUserDefaults*)defaultsPtr;
NSString *value = [defaults stringForKey:[NSString stringWithUTF8String:key]];
const char *cValue = [value UTF8String];
return cValue;
}
22 changes: 22 additions & 0 deletions userDefaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gocoa

import (
"testing"
)

func TestNewPUserDefaults(t *testing.T) {
defaults := NewPUserDefaults()
if defaults.userDefaultsPtr == nil {
t.Fatalf("pointer to C defaults is nil!")
}
}

func TestStringValue(t *testing.T) {
defaults := NewPUserDefaults()
value := "FooBar!"
defaults.SetString("TestKey", value)
readValue := defaults.GetString("TestKey")
if readValue != value {
t.Fatalf("read value should be %s, but is %s", value, readValue)
}
}