TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Interacting with C Pointers

48 点作者 speednoise将近 11 年前

3 条评论

msarnoff将近 11 年前
I&#x27;m a little disappointed with the scope of this writeup. I just finished my first full Swift app this weekend, and by far, the most bothersome part was interacting with the C APIs (CoreMedia, ImageIO, etc.) I would have loved to see more examples of interacting with framework objects like CGImageSources, CMSampleBuffers, etc.<p>For example, CGImageSourceCreateWithURL() returns an Unmanaged&lt;CGImageSource&gt;!. You can&#x27;t pass that object to CGImageSourceCreateImageAtIndex(), because it expects a CGImageSource!. Instead, you have to call takeUnretainedValue() on the Unmanaged to extract the raw pointer. Not terribly difficult, just mildly annoying and not well documented. In C&#x2F;ObjC, you&#x27;d just take the return value of the first function and pass it right into the second.<p>Another example: CMSampleBufferGetImageBuffer() returns a CVImageBuffer!. And CVPixelBufferLockBaseAddress() takes a CVPixelBuffer! as its argument. In C&#x2F;ObjC, CVPixelBufferRef is typedef&#x27;d to CVImageBufferRef, but that doesn&#x27;t carry over in Swift. I tried to figure out the opaque-pointer voodoo required to convert from one to the other, and eventually gave up, wrote the function in Objective-C, and moved on.<p>Swift is shaping up to be an excellent language, but at this point, the casting&#x2F;wrapping&#x2F;unwrapping hurdles make it difficult for newcomers and experienced developers alike to take advantage of Apple&#x27;s 15+ year corpus of powerful APIs.
评论 #8099648 未加载
评论 #8100433 未加载
plus9z将近 11 年前
&quot;For safety, Swift requires the variables to be initialized before being passed with &amp;. This is because it cannot know whether the method being called tries to read from a pointer before writing to it.&quot;<p>I&#x27;ve not written any Swift code yet, so can someone enlighten me -- is this actually true? This seems unnecessarily restrictive (and absurd -- Swift can&#x27;t tell if a pointer&#x27;s being read?)
评论 #8099939 未加载
评论 #8099927 未加载
评论 #8100125 未加载
nextstep将近 11 年前
&gt;&gt; For safety, Swift requires the variables to be initialized before being passed with &amp;. This is because it cannot know whether the method being called tries to read from a pointer before writing to it.<p>Isn&#x27;t the &#x27;maybeError&#x27; var being used uninitialized in the example right about this passage?
评论 #8100412 未加载