gpt4 book ai didi

objective-c - Swift:如何通过 SCDynamicStoreCopyConsoleUser 获取控制台用户、UID 和 GID?

转载 作者:行者123 更新时间:2023-11-30 10:23:13 25 4
gpt4 key购买 nike

我可以使用 python 从 SCDynamicStoreCopyConsoleUser 获取用户名、UID 和 GID:

 #!/usr/bin/python
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
cfuser = SCDynamicStoreCopyConsoleUser( None, None, None )
print cfuser[0] # Returns console user, e.g.: myUsername
print cfuser[1] # Returns console user’s UID, e.g.: 501
print cfuser[2] # Returns console user’s GID, e.g.: 20

如何使用 Swift 获得相同的返回?

SCDynamicStoreCopyConsoleUser 的 Swift 声明

 func SCDynamicStoreCopyConsoleUser(_ store: SCDynamicStore!,
_ uid: CMutablePointer<uid_t>,
_ gid: CMutablePointer<gid_t>) -> Unmanaged<CFString>!

我的快速通话

 var uid: CMutablePointer<uid_t>!
var gid: CMutablePointer<gid_t>!

var cfuser: NSArray = [SCDynamicStoreCopyConsoleUser(nil,uid,gid)]

// the return has only one element containing the username

最佳答案

Swift 函数 SCDynamicStoreCopyConsoleUser需要 CMutablePointer<uid_t>参数意味着您必须传递 uid_t地址多变的。另外你应该检查调用是否成功(否则返回nil),你呢必须按照中所述转换返回的非托管对象 Working with Cocoa Data Types :

import SystemConfiguration

var uid: uid_t = 0
var gid: gid_t = 0

if let theResult = SCDynamicStoreCopyConsoleUser(nil, &uid, &gid) {
let name = theResult.takeUnretainedValue()
println("name = \(name), uid = \(uid), gid = \(gid)")
} else {
println("failed")
}

关于objective-c - Swift:如何通过 SCDynamicStoreCopyConsoleUser 获取控制台用户、UID 和 GID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24591760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com