创建UnsafePointer <unsafemutablepointer <int8>&gt;来自[String]

时间:2018-01-08 10:59:15

标签: swift macos pointers

快速成长,指针和低级c的概念对我来说并不熟悉,至少对我来说,很难理解。我有一个Array<String>,我需要创建一个指针(这就是我如何理解指针)。函数UnsafePointer<UnsafeMutablePointer<Int8>>需要类型AuthorizationExecuteWithPrivileges()的指针。我阅读了关于在swift和使用google中使用指针的raywenderlich-guide,但我仍然无法让它工作。 这是我的代码(我主要是从here借来的):

func runAsAdmin(command: String, arguments: [String]) {

    var status = OSStatus()
    var authorizationRef: AuthorizationRef?


    status = AuthorizationCreate(nil, nil, [], &authorizationRef)

    if status != errAuthorizationSuccess {
        print("Error Creating Initial Authorization: \(status)")
    }

    let authFlags: AuthorizationFlags = [.interactionAllowed, .preAuthorize, .extendRights]
    var rightItems = [AuthorizationItem(name: kAuthorizationRightExecute, valueLength: 0, value: nil, flags: 0)]
    var rights = AuthorizationRights.init(count: UInt32(rightItems.count), items: &rightItems)

    status = AuthorizationCopyRights(authorizationRef!, &rights, nil, authFlags, nil)

    if status != errAuthorizationSuccess {
        print("Copy Rights Unsuccessful: \(status)")
    }

    var pipe: FILE?
    let tool = UnsafePointer<Int8>((command as NSString).utf8String!) //that seems good
    var pToArgs = arguments.flatMap({($0 as NSString).utf8String}) //thats where the pain starts
    var buffer = pre.withUnsafeMutableBufferPointer { // not sure about this approach, probably only returns the first value
        return $0.baseAddress
    }

    status = AuthorizationExecuteWithPrivileges(authorizationRef!, tool, authFlags, buffer!, &pipe)


}

有什么建议吗?

0 个答案:

没有答案