Skip to content

Commit

Permalink
refactor: modify swift api depend on "API Design Guidelines"
Browse files Browse the repository at this point in the history
  • Loading branch information
Damonvvong committed Sep 26, 2018
1 parent 6556640 commit f78e1f5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 71 deletions.
66 changes: 35 additions & 31 deletions iOS/MMKV/MMKV/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,74 @@

@interface MMKV : NSObject

NS_ASSUME_NONNULL_BEGIN

// a generic purpose instance
+ (instancetype)defaultMMKV;

// mmapID: any unique ID (com.tencent.xin.pay, etc)
// if you want a per-user mmkv, you could merge user-id within mmapID
+ (instancetype)mmkvWithID:(NSString *)mmapID;
+ (nullable instancetype)mmkvWithID:(NSString *)mmapID NS_SWIFT_NAME(init(mmapID:));

// mmapID: any unique ID (com.tencent.xin.pay, etc)
// if you want a per-user mmkv, you could merge user-id within mmapID
// cryptKey: 16 byte at most
+ (instancetype)mmkvWithID:(NSString *)mmapID cryptKey:(NSData *)cryptKey;
+ (nullable instancetype)mmkvWithID:(NSString *)mmapID cryptKey:(NSData *)cryptKey NS_SWIFT_NAME(init(mmapID:cryptKey:));

- (BOOL)reKey:(NSData *)newKey;
- (BOOL)reKey:(NSData *)newKey NS_SWIFT_NAME(reset(cryptKey:));
- (NSData *)cryptKey;

// object: NSString/NSData/NSDate
- (BOOL)setObject:(id)obj forKey:(NSString *)key;

- (BOOL)setBool:(BOOL)value forKey:(NSString *)key;
- (BOOL)setObject:(id)object forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setInt32:(int32_t)value forKey:(NSString *)key;
- (BOOL)setBool:(BOOL)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key;
- (BOOL)setInt32:(int32_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setInt64:(int64_t)value forKey:(NSString *)key;
- (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key;
- (BOOL)setInt64:(int64_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setFloat:(float)value forKey:(NSString *)key;
- (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setDouble:(double)value forKey:(NSString *)key;
- (BOOL)setFloat:(float)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (id)getObjectOfClass:(Class)cls forKey:(NSString *)key;
- (BOOL)setDouble:(double)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (bool)getBoolForKey:(NSString *)key;
- (bool)getBoolForKey:(NSString *)key defaultValue:(bool)defaultValue;
- (nullable id)getObjectOfClass:(Class)cls forKey:(NSString *)key NS_SWIFT_NAME(object(of:forKey:));

- (int32_t)getInt32ForKey:(NSString *)key;
- (int32_t)getInt32ForKey:(NSString *)key defaultValue:(int32_t)defaultValue;
- (bool)getBoolForKey:(NSString *)key NS_SWIFT_NAME(boolValue(forKey:));
- (bool)getBoolForKey:(NSString *)key defaultValue:(bool)defaultValue NS_SWIFT_NAME(boolValue(forKey:defaultValue:));

- (uint32_t)getUInt32ForKey:(NSString *)key;
- (uint32_t)getUInt32ForKey:(NSString *)key defaultValue:(uint32_t)defaultValue;
- (int32_t)getInt32ForKey:(NSString *)key NS_SWIFT_NAME(int32(forKey:));
- (int32_t)getInt32ForKey:(NSString *)key defaultValue:(int32_t)defaultValue NS_SWIFT_NAME(int32(forKey:defaultValue:));

- (int64_t)getInt64ForKey:(NSString *)key;
- (int64_t)getInt64ForKey:(NSString *)key defaultValue:(int64_t)defaultValue;
- (uint32_t)getUInt32ForKey:(NSString *)key NS_SWIFT_NAME(uint32(forKey:));
- (uint32_t)getUInt32ForKey:(NSString *)key defaultValue:(uint32_t)defaultValue NS_SWIFT_NAME(uint32(forKey:defaultValue:));

- (uint64_t)getUInt64ForKey:(NSString *)key;
- (uint64_t)getUInt64ForKey:(NSString *)key defaultValue:(uint64_t)defaultValue;
- (int64_t)getInt64ForKey:(NSString *)key NS_SWIFT_NAME(int64(forKey:));
- (int64_t)getInt64ForKey:(NSString *)key defaultValue:(int64_t)defaultValue NS_SWIFT_NAME(int64(forKey:defaultValue:));

- (float)getFloatForKey:(NSString *)key;
- (float)getFloatForKey:(NSString *)key defaultValue:(float)defaultValue;
- (uint64_t)getUInt64ForKey:(NSString *)key NS_SWIFT_NAME(uint64(forKey:));
- (uint64_t)getUInt64ForKey:(NSString *)key defaultValue:(uint64_t)defaultValue NS_SWIFT_NAME(uint64(forKey:defaultValue:));

- (float)getFloatForKey:(NSString *)key NS_SWIFT_NAME(float(forKey:));
- (float)getFloatForKey:(NSString *)key defaultValue:(float)defaultValue NS_SWIFT_NAME(float(forKey:defaultValue:));

- (double)getDoubleForKey:(NSString *)key;
- (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue;
- (double)getDoubleForKey:(NSString *)key NS_SWIFT_NAME(double(forKey:));
- (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue NS_SWIFT_NAME(double(forKey:defaultValue:));

- (BOOL)containsKey:(NSString *)key;
- (BOOL)containsKey:(NSString *)key NS_SWIFT_NAME(contains(key:));

- (size_t)count;

- (size_t)totalSize;

- (void)enumerateKeys:(void (^)(NSString *key, BOOL *stop))block;

- (void)removeValueForKey:(NSString *)key;
- (void)removeValueForKey:(NSString *)key NS_SWIFT_NAME(removeValue(forKey:));

- (void)removeValuesForKeys:(NSArray *)arrKeys;
- (void)removeValuesForKeys:(NSArray *)arrKeys NS_SWIFT_NAME(removeValues(forKeys:));

- (void)clearMemoryCache;

Expand All @@ -98,6 +100,8 @@
- (void)sync;

// for CrashProtected Only!!
+ (BOOL)isFileValid:(NSString *)mmapID;
+ (BOOL)isFileValid:(NSString *)mmapID NS_SWIFT_NAME(isFileValid(for:));

NS_ASSUME_NONNULL_END

@end
6 changes: 3 additions & 3 deletions iOS/MMKV/MMKV/MMKV.mm
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,11 @@ - (BOOL)reKey:(NSData *)newKey {

#pragma mark - set & get

- (BOOL)setObject:(id)obj forKey:(NSString *)key {
if (obj == nil || key.length <= 0) {
- (BOOL)setObject:(id)object forKey:(NSString *)key {
if (object == nil || key.length <= 0) {
return NO;
}
NSData *data = [MiniPBCoder encodeDataWithObject:obj];
NSData *data = [MiniPBCoder encodeDataWithObject:object];
return [self setData:data forKey:key];
}

Expand Down
77 changes: 40 additions & 37 deletions iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,45 @@ import Foundation

@objc class DemoSwiftUsage : NSObject {
func testSwiftFunctionality() {
let mmkv = MMKV(id: "testSwift")!;

mmkv.setBool(true, forKey: "bool");
print("Swift: bool = \(mmkv.getBoolForKey("bool"))");

mmkv.setInt32(-1024, forKey: "int32");
print("Swift: int32 = \(mmkv.getInt32ForKey("int32"))");

mmkv.setUInt32(UInt32.max, forKey: "uint32");
print("Swift: uint32 = \(mmkv.getUInt32(forKey: "uint32"))");

mmkv.setInt64(Int64.min, forKey: "int64");
print("Swift: int64 = \(mmkv.getInt64ForKey("int64"))");

mmkv.setUInt64(UInt64.max, forKey: "uint64");
print("Swift: uint64 = \(mmkv.getUInt64(forKey:"uint64"))");

mmkv.setFloat(-3.1415926, forKey: "float");
print("Swift: float = \(mmkv.getFloatForKey("float"))");

mmkv.setDouble(Double.infinity, forKey: "double");
print("Swift: double = \(mmkv.getDoubleForKey("double"))");

mmkv.setObject("Hello from Swift", forKey: "string");
print("Swift: string = \(String(describing: mmkv.getObjectOf(NSString.self, forKey:"string")))");

mmkv.setObject(NSDate(), forKey: "date");
let date = mmkv.getObjectOf(NSDate.self, forKey:"date") as! Date;
print("Swift: date = \(date.description(with: .current))");

mmkv.setObject("Hello from Swift".data(using: .utf8), forKey: "data");
let data = mmkv.getObjectOf(NSData.self, forKey:"data");
let str = String(data: data as! Data, encoding: .utf8);
print("Swift: data = \(String(describing: str))");

mmkv.removeValue(forKey: "bool");
print("Swift: after delete bool = \(mmkv.getBoolForKey("bool"))");

guard let mmkv = MMKV(mmapID: "testSwift") else {
return
}

mmkv.set(true, forKey: "bool")
print("Swift: bool = \(mmkv.boolValue(forKey: "bool"))")

mmkv.set(Int32(-1024), forKey: "int32")
print("Swift: int32 = \(mmkv.int32(forKey: "int32"))")

mmkv.set(UInt32.max, forKey: "uint32")
print("Swift: uint32 = \(mmkv.uint32(forKey: "uint32"))")

mmkv.set(Int64.min, forKey: "int64")
print("Swift: int64 = \(mmkv.int64(forKey: "int64"))")

mmkv.set(UInt64.max, forKey: "uint64")
print("Swift: uint64 = \(mmkv.uint64(forKey: "uint64"))")

mmkv.set(Float(-3.1415926), forKey: "float")
print("Swift: float = \(mmkv.float(forKey: "float"))")

mmkv.set(Double.infinity, forKey: "double")
print("Swift: double = \(mmkv.float(forKey: "double"))")

mmkv.set("Hello from Swift", forKey: "string")
print("Swift: string = \(mmkv.object(of: NSString.self, forKey: "string") ?? "")")

mmkv.set(NSDate(), forKey: "date")
let date = mmkv.object(of: NSDate.self, forKey: "date") as? Date
print("Swift: date = \(date?.description(with: .current) ?? "null")")

mmkv.set("Hello from Swift".data(using: .utf8) ?? Data(), forKey: "data")
let data = mmkv.object(of: NSData.self, forKey: "data") as? Data
let str = String(data: data ?? Data(), encoding: .utf8) ?? ""
print("Swift: data = \(str)")

mmkv.removeValue(forKey: "bool")
print("Swift: after delete bool = \(mmkv.boolValue(forKey: "bool"))")
}
}

0 comments on commit f78e1f5

Please sign in to comment.