我想把代码目标C转换成Kotlin多平台这是目标C代码
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL isDir;
BOOL exists = [fileManager fileExistsAtPath:path isDirectory:&isDir];
BOOL success = false;
if (isDir && exists) {
NSURL *pathUrl = [NSURL fileURLWithPath:path];
NSError *error = nil;
success = [pathUrl setResourceValue:isExcluded
forKey:NSURLIsExcludedFromBackupKey
error:&error];
if (!success) {
NSLog(@"Could not exclude AsyncStorage dir from backup %@", error);
}
}
return success;
}在上面的代码变量中,isDir使用&当传递到函数fileExistsAtPath时,我如何在Kotlin中这样使用isDir。我知道这是address-of unary operator
我能用一下吗
val isDir: CPointer<BooleanVar /* = kotlinx.cinterop.BooleanVarOf<kotlin.Boolean> */>? = null
val exists = fileManager?.fileExistsAtPath(path, isDir)发布于 2022-03-30 05:30:58
要在Kotlin中创建指针,需要memScoped,在此范围内可以调用alloc来创建任意类型的指针。
下面是如何用Kotlin编写代码:
return memScoped {
val fileManager = NSFileManager.defaultManager
val isDir = alloc<BooleanVar>()
val exists = fileManager.fileExistsAtPath(path, isDirectory = isDir.ptr)
var success = false
if (exists && isDir.value) {
val pathUrl = NSURL.fileURLWithPath(path)
val error = alloc<ObjCObjectVar<NSError?>>()
success = pathUrl.setResourceValue(isExcluded, forKey = NSURLIsExcludedFromBackupKey, error = error.ptr)
if (!success) {
println("Could not exclude AsyncStorage dir from backup ${error.value}")
}
}
return@memScoped success
}https://stackoverflow.com/questions/71671601
复制相似问题