如何使用UnsafeMutablePointer <unsafemutablepointer <void>&gt;来自Swift的参考?

时间:2015-10-21 00:33:58

标签: ios swift scenekit

我正在使用SceneKit的SCNParticleSystem构建测试应用。它有一个回调,允许您修改每个帧上的粒子属性。此回调的签名是

typealias SCNParticleModifierBlock = (UnsafeMutablePointer<UnsafeMutablePointer<Void>>, UnsafeMutablePointer<Int>, Int, Int, Float) -> Void

来自Apple开发者网站的参考资料 - SCNParticleSystem_Class

我不知道如何从Swift访问和修改此引用。如果这是C,那么我可以像数组一样取消引用**

在经历了一些困难之后,我已经达到了这个目标:

..... 
           particleSystem?.addModifierForProperties([SCNParticlePropertySize], atStage: SCNParticleModifierStage.PostDynamics, withBlock: doit2)
}

struct Foos {
    var size:float_t
}
func doit2(data:UnsafeMutablePointer<UnsafeMutablePointer<Void>>, dataStride: UnsafeMutablePointer<Int>, start:Int, end:Int, deltaTime:Float) -> Void {
    let myptr = UnsafeMutablePointer<UnsafeMutablePointer<Foos>>(data)
    print("indexes",start,end)
    for i in 0 ..< end {
        print(i,myptr[i].memory.size)
    }
}¸

这适用于第一个粒子,但在第二个粒子上崩溃。第一次调用该函数时,有0个粒子,因此它会跳过循环。第二次有三个粒子,所以它试图将它们打印出来。第一个尺寸值为0.9,接缝合理。第二个大小值显然是假的,然后它崩溃了,我进入调试器。

indexes 0 0
indexes 0 3
0 0.929816
1 1.51296e-39
(lldb)

尽我所知,互联网上没有人使用此功能。我找到的唯一参考文献是Apple的文档,它只提供了ObjC示例,而不是Swift。

帮助我!

2 个答案:

答案 0 :(得分:1)

为考试:

var data = [[0.1, 0.2],[0.3, 0.4],[0.5, 0.6]]
let pData = UnsafeMutablePointer<UnsafeMutablePointer<Void>>(data)
// how to reconstruct the original data ??

// we need to know how much data we have
let count = data.count
// we need to know what type of data we have
let p2 = UnsafeMutablePointer<Array<Double>>(pData)
// access the data
for i in 0..<count {
    print((p2 + i).memory)
}
// [0.1, 0.2]
// [0.3, 0.4]
// [0.5, 0.6]

我认为在你的代码中,myptr的声明是错误的

let myptr = UnsafeMutablePointer<UnsafeMutablePointer<Foos>>(data)
示例中的

dataStride.count应为1(属性数),其元素的值应为float的大小(属性的大小)。

也要小心!你的循环应该像

for i in start..<end {
...
}

你确定开始是0 ???

答案 1 :(得分:0)

使用非常相似的SCNParticleEventBlock,我在Swift3中编写了处理程序

from selenium import webdriver
import time

def linked_in():

    driver = webdriver.Chrome()
    driver.get('https://www.linkedin.com/uas/login?goback=&trk=hb_signin')

    driver.find_element_by_xpath('//*[@id="session_key-login"]').send_keys('email')
    driver.find_element_by_xpath('//*[@id="session_password-login"]').send_keys('password')
    driver.find_element_by_xpath('//*[@id="btn-primary"]').click()
    time.sleep(5)

    driver.find_element_by_xpath("//a[contains(@class,'feed-s-follows-module__view-all')]").click()
    time.sleep(5)

    for item in driver.find_elements_by_xpath("//a[contains(@class,'feed-s-follow-recommendation-card__profile-link')]"):
        arranging_items(driver, item.get_attribute("href"))

def arranging_items(driver, name_link):

    driver.get(name_link)


    try:
        for item in driver.find_elements_by_xpath("//div[contains(@class,'org-top-card-module__details')]"):
            name = item.find_element_by_xpath(".//h1[@title]")
            title = item.find_element_by_xpath(".//span[contains@class,'company-industries']")
    except Exception:
        pass
    else:
        try:
            for item in driver.find_elements_by_xpath("//div[contains(@class,'pv-top-card-section__information')]"):
                name = item.find_element_by_xpath(".//h1[contains(@class,'pv-top-card-section__name')]")
                title = item.find_element_by_xpath(".//h2[contains(@class,'pv-top-card-section__headline')]")
        except Exception:
            pass
    finally:
        try:
            print(name.text, title.text)
        except Exception:
            pass

    driver.quit()

linked_in()

我的SO中的更多详细信息询问并回答了问题here

还有几个要点herehere