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

关于 API 设计的一些疑惑 #42

Closed
Damonvvong opened this issue Sep 27, 2018 · 8 comments
Closed

关于 API 设计的一些疑惑 #42

Damonvvong opened this issue Sep 27, 2018 · 8 comments

Comments

@Damonvvong
Copy link
Contributor

Damonvvong commented Sep 27, 2018

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

支持的类型只有 NSString/NSData/NSDate 这三种,为什么不是分成三个不同的方法而是采用通用 id 来处理呢?

- (BOOL)setString:(NSString *)string forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));
- (BOOL)setDate:(NSDate *)date forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));
- (BOOL)setData:(NSData *)data forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));
@Damonvvong
Copy link
Contributor Author

如果采用上面的设计。

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

就可以采用另外的方式实现。

- (BOOL)setObject:(id<NSCoding>)value forKey:(NSString *)key {
    if (key.length <= 0) {
        return NO;
    }
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:obj];
    return [self setData:data forKey:key];
}

@Damonvvong
Copy link
Contributor Author

Damonvvong commented Sep 27, 2018

这样子在 getValue 的时候不再需要传入对应的 class 了。

- (id)getObjectForKey:(NSString *)key {
    if (key.length <= 0) {
        return nil;
    }
    NSData *data = [self getDataForKey:key];
    if (data.length > 0) {
        return [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    return nil;
}

@lingol
Copy link
Collaborator

lingol commented Sep 27, 2018

Because we don't want anything to do with NSCoding. MMKV is designed to be generic purpose. Being generic means you can archive your object into data by using any algorithm you like, such as protobuf. In fact, we get something called PBCoding inside WeChat, which looks like NSCoding, but use protobuf protocol to archive/unarchive object.

As #10 has suggested, we might add a setObject:(id<NSCoding>)value forKey:(NSString*)key interface in the future. That is under consideration.

@Damonvvong
Copy link
Contributor Author

恩,我大致了解了你们内部有一个 PBCoding 的东西。所以理解了你们做的方式。

但是开源的部分是没有 PBCoding 的。所以现在使用起来相对是有点变扭的。你觉得呢?

期待后续改动,我先自己项目适配一下吧~

@Damonvvong
Copy link
Contributor Author

可以有新的 pod 版本吗?我这边要做一些项目定制化,需要一个支持 Swift 改版的 pod

在 podspec 中无法指定分支。

@lingol
Copy link
Collaborator

lingol commented Sep 27, 2018

可以有新的 pod 版本吗?我这边要做一些项目定制化,需要一个支持 Swift 改版的 pod

在 podspec 中无法指定分支。

The changes made on Swift interface has been merged into master. But a new release is not ready yet. For the time being, you can change your pod file as bellow:
pod 'MMKV', :git => 'https://github.com/Tencent/MMKV.git'
A fresh pull of master should suit your need.

@Damonvvong
Copy link
Contributor Author

恩恩,辛苦了~

@lingol
Copy link
Collaborator

lingol commented Oct 9, 2018

Supported with #78 , checkout release note.

@sven-brobonds sven-brobonds mentioned this issue Jul 21, 2020
This was referenced Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants