所以我试图在我的Swift iOS应用程序中的SLRequest对象上使用performRequestWithHandler块,我无法处理NSError对象.这就是我的代码的样子:
posts.performRequestWithHandler({(response:NSData!, urlResponse:NSHTTPURLResponse!, error:NSError!) in
self.data = NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableLeaves, error: &error)
})
Run Code Online (Sandbox Code Playgroud)
我&error说错了:'NSError' is not convertible to '@lvalue inout $T9' in Swift.有谁知道这意味着什么?
先感谢您.
(我正在使用Xcode Beta 6 v7和OS X 10.10)
我正在为我们的代码库中的性能/内存关键部分试验数据结构.我想快速访问结构中定义的字节.但是我不知道如何使用索引器访问我正在操作的结构.
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Foo
{
[SerializeField]
private byte a, b, c;
public unsafe byte this[byte index]
{
get
{
//omitted safety checks
//this is a no, no
byte* addr = (byte*)&this;
return addr[index];
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用外部提供的C库快速解析FIT文件的库。解析函数将a作为参数void * data。为了调用该函数,我使用转换数据data.withUnsafeBytes( { (ptr: UnsafePointer<UInt8>) in ...}以建立c函数的参数,并且可以正常工作。
将Xcode升级到Swift 5之后,我现在收到了已弃用的警告
不建议使用“ withUnsafeBytes”:请withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R改用
我无法解决如何修复代码以删除不推荐使用的警告。代码运行正常,并且没有迅速发出警告4
我试图更改闭包中的参数UnsafeRawBufferPointer而不是UnsafePointer,但这导致调用该函数时出错:Cannot convert 'UnsafeRawBufferPointer' to expected argument type 'UnsafeRawPointer?'
这是一个小的swift文件,用于显示问题:
import Foundation
// Create sample data (Typically would be read from a file
let data = Data(repeating: 1, count: 10)
data.withUnsafeBytes( { (ptr : UnsafePointer<UInt8>) in
// call the c function with the void* argument
let value = readFITfile(ptr)
print( value ) …Run Code Online (Sandbox Code Playgroud) Swift中的代码
...
var time:timeval?
gettimeofday(UnsafePointer<timeval>, UnsafePointer<()>) // this is the method expansion before filling in any data
...
Run Code Online (Sandbox Code Playgroud)
目标C中的代码
...
struct timeval time;
gettimeofday(&time, NULL);
...
Run Code Online (Sandbox Code Playgroud)
我一直在试图找到有关UnsafePointer的更多信息以及传递NULL的替代方法,但我可能正在咆哮错误的树.
如果有人知道如何在Swift中使用等效代码,那就太好了.如果有一个很好的解释,它会发生什么,甚至更好!
我试图在Swift中创建CICrossPolynomial过滤器类型.
我不确定如何创建语法来执行此操作.
文档指定了一个CIVector,它有一个浮点数组?
A CIVector object whose display name is RedCoefficients.
Default value: [1 0 0 0 0 0 0 0 0 0] Identity: [1 0 0 0 0 0 0 0 0 0]
Run Code Online (Sandbox Code Playgroud)
但是我该如何宣布这样的CIVector呢?有一个具有此签名的构造函数
CIVector(values: <UnsafePointer<CGFloat>>, count: <UInt>)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试
var floatArr:Array<CGFloat> = [1,0,0,0,0,0,0,0,0]
var vector = CIVector(values: floatArr, count: floatArr.count)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Cannot invoke 'init' with an argument list type (values: @lvaue Array<CGFloat>, count:Int)
Run Code Online (Sandbox Code Playgroud)
你知道如何用一系列CGFloats正确创建一个CIVector吗?
有人可以向我解释我在这里缺少什么吗?
给定的
let index: Int32 = 100
Run Code Online (Sandbox Code Playgroud)
为什么这不行:
// Use of extraneous '&'
let ptr = &index // Type inference?
Run Code Online (Sandbox Code Playgroud)
甚至:
// Use of extraneous '&'
let ptr: UnsafePointer<Int32> = &index
Run Code Online (Sandbox Code Playgroud)
但这是:
{
func point(num: UnsafePointer<Int32>) -> UnsafePointer<Int32> {
return num
}
let ptr = point(num: &index)
}
Run Code Online (Sandbox Code Playgroud)
这将是 C 中的简单等价物:
int index = 100;
int *ptr = &index;
Run Code Online (Sandbox Code Playgroud)
我真的必须定义一个函数,从字面上获取引用的值并传回完全相同的引用吗?感觉有些不对劲。似乎我在这里遗漏了一些东西,甚至可能是根本的。
如何将 UnsafePointer 分配给它所在类型的内存地址(在本例中为 Int32)???
谢谢!
编辑:
最终我试图完成的是,我需要将几种不同的结构写入一个二进制文件。变量index将是结构的属性。我现在要走的路径涉及一个文件OutputStream。我不介意收到关于此的建议,但超出了原始问题的范围。
我在玩不安全的Rust时遇到了这种奇怪的现象.我认为这段代码应该会出现分段错误,但事实并非如此.我错过了什么吗?我试图设置一个指向一个生命周期较短的变量的指针,然后取消引用它.
// function that sets a pointer to a variable with a shorter lifetime
unsafe fn what(p: &mut *const i32) {
let a = 2;
*p = &a;
//let addr = *p; // I will talk about this later
println!("inside: {}", **p);
}
fn main() {
let mut p: *const i32 = 0 as *const i32;
unsafe {
what(&mut p);
// I thought this line would make a segfault because 'a' goes out of scope at the end of the …Run Code Online (Sandbox Code Playgroud) 我已经创建了这样的数组
var outputReal = UnsafeMutablePointer<Double>.allocate(capacity: numeroDados)
Run Code Online (Sandbox Code Playgroud)
现在,我需要将其转换为的数组Double。
我可以使用以下方式进行转换:
var newArray : [Double] = []
for i in 0..<n {
newArray[i] = outputReal
}
Run Code Online (Sandbox Code Playgroud)
但是我记得在页面上看到了另一种方法。
有任何想法吗?
我正在尝试使用windows.CreateFile()函数创建文件(有关参考,请参阅https://godoc.org/golang.org/x/sys/windows#CreateFile和https://docs.microsoft.com/en-us/windows/ win32/api/fileapi/nf-fileapi-createfilew ) 在 Golang 1.14 中。除了代码有效之外,我显然file Name为CreateFile().
代码是:
package main
import (
"unsafe"
"golang.org/x/sys/windows"
)
func main() {
var (
nullHandle windows.Handle
filename string = "test_file"
)
strptr := &filename
fileNamePtr := (*uint16)(unsafe.Pointer(strptr))
dwShareMode := uint32(windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_DELETE)
dwFlagsAndAttributes := uint32(windows.FILE_FLAG_DELETE_ON_CLOSE)
windows.CreateFile(fileNamePtr, windows.GENERIC_WRITE, dwShareMode, nil, windows.CREATE_NEW, dwFlagsAndAttributes, nullHandle)
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个用非 ascii 字符创建的文件(在这种情况下?R)
Directory of C:\Users\rodrigo\src\delete_on_close
04/30/2020 03:15 PM <DIR> .
04/30/2020 03:15 PM <DIR> ..
04/30/2020 …Run Code Online (Sandbox Code Playgroud) 我拥有的:
引用Apple 的 Chroma Key Code,它指出我们可以通过创建一个 Chroma Key Filter Cube
func chromaKeyFilter(fromHue: CGFloat, toHue: CGFloat) -> CIFilter?
{
// 1
let size = 64
var cubeRGB = [Float]()
// 2
for z in 0 ..< size {
let blue = CGFloat(z) / CGFloat(size-1)
for y in 0 ..< size {
let green = CGFloat(y) / CGFloat(size-1)
for x in 0 ..< size {
let red = CGFloat(x) / CGFloat(size-1)
// 3
let hue = getHue(red: red, green: green, …Run Code Online (Sandbox Code Playgroud)