草庐IT

int_void

全部标签

Swift 编译器错误 Int 不可转换为 CGFloat

我试图运行这段代码,但编译器一直在烦我Int无法转换为CGFloat,但我从未将min、max或value声明为变量“Int”,之前也未提及它们。overridefunctouchesBegan(touches:NSSet,withEventevent:UIEvent){/*Calledwhenatouchbegins*/bird.physicsBody.velocity=CGVectorMake(0,0)bird.physicsBody.applyImpulse(CGVectorMake(0,8))}funcclamp(min:CGFloat,max:CGFloat,value:CG

swift - 如何在 Swift 中将 [Int8] 转换为 [UInt8]

我有一个只包含字符的缓冲区letbuffer:[Int8]=....然后我需要将其传递给函数process,该函数将[UInt8]作为参数。funcprocess(buffer:[UInt8]){//somecode}传递[Int8]缓冲区以转换为[Int8]的最佳方法是什么?我知道下面的代码可以工作,但在这种情况下,缓冲区只包含一堆字符,没有必要使用像map这样的函数。process(buffer.map{xinUInt8(x)})//OKprocess([UInt8](buffer))//errorprocess(bufferas![UInt8])//error我正在使用Xcode

Swift - 可选的 Void

我正忙于使用NSURLProtocolClient的URLProtocol函数:welf?.client?.URLProtocol(welf!,didReceiveResponse:operation.response,cacheStoragePolicy:NSURLCacheStoragePolicy.NotAllowed)我原以为它会返回Void。但令我惊讶的是它返回Void?为什么要区分Void和Void?我读到Void是空元组类型的类型别名。那么,这是否与空元组类型与nil之间的区别有关? 最佳答案 这仅仅是因为您正在使用O

ios - 类型 'Int32' 不符合协议(protocol) 'AnyObject' Swift?

我有一个模型,NSObject的子类,如下所示。classConfigDao:NSObject{varcategoriesVer:Int32=Int32()varfireBallIP:String=String()varfireBallPort:Int32=Int32()varisAppManagerAvailable:Bool=Bool()vartimePerQuestion:String=String()varisFireballAvailable:Bool=Bool()}我已经下载了NSMutableData并使用NSJSONSerialization从中生成了JSON。我的代码

Swift 3 - 通过 Int 属性减少对象集合

我有一个包含3个对象的数组,如下所示:classAClass{vardistance:Int?}letobj0=AClass()obj0.distance=0letobj1=AClass()obj1.distance=1letobj2=AClass()obj2.distance=2letarr=[obj0,obj1,obj2]当我减少数组并将其分配给一个变量时,我只能对数组中的最后一个元素求和。lettotal=arr.reduce(0,{$1.distance!+$1.distance!})//returns4如果我尝试$0.distance!它错误为“表达式不明确,没有更多上下文”

arrays - 如何在 swift 5.0 的数组中找到最小的非零 INT?

我有一些数组,我需要在每一行中找到最小的非零整数。有没有办法用min(by:)做到这一点?例如varrow=[0,0,0,0,0,0,0,0,1,3,5,6,9]所以我需要得到1通过row.min()我总是得到0。有人告诉我可以用min{by:}来完成,但我不完全理解语法。 最佳答案 您可以过滤数组以获得所需的值,并使用Array.min()方法,就像这样row.filter{$0>0}.min()或者以下将仅在数组有有序数字时起作用row.first(where:{$0>0}) 关于a

pointers - 在 swift 中将 CFContextRef 转换为可变的 void 指针?

我正在尝试将此objc代码转换为swift:CGContextRefcontextRef=CGBitmapContextCreate(NULL,proposedRect.size.width,proposedRect.size.height,CGImageGetBitsPerComponent(imageRef),0,colorSpaceRef,(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);NSGraphicsContext*context=[NSGraphicsContextgraphicsContextWithGraphicsPort

swift - 将 Int 保存到核心数据中的正确方法?

我有一个具有Int16属性的实体。我想保存varuserRepsCount=Int(),这是由下面的步进方法设置的:@IBActionfuncuserRepsStepper(_sender:UIStepper){userExerciseRepsCounter.text=Int(sender.value).descriptionself.userRepsCount=Int(sender.value)}我想将它添加到属性中并使用userExercise.reps=userRepsCount来这样做,但是我得到了错误Cannotassignvalueoftype'Int'totype'Int

swift - 为什么 Int.random() 比 arc4random_uniform() 慢?

我已经使用Int.random()方法和arc4random_uniform()进行数字生成速度测试。这两个测试都在macOS控制台中运行,构建配置设置为发布。以下是我用于测试的代码。publicfuncrandomGen1(){letn=1_000_000letstartTime=CFAbsoluteTimeGetCurrent()foriin0..我得到的时间是0.029475092887878418(对于arc4random_uniform(10))0.20298802852630615(对于Int.random(in:0..为什么Int.random()这么慢?有什么办法可以优

objective-c - 是否可以将 Swift 类转换为 C void* 指针?

是否可以将Swift类转换为Cvoid*指针?//swiftclassclassMyClass{}varmyClass=MyClass()varp:UnsafeMutablePointer=myClass//谢谢 最佳答案 我遇到了类似的问题,你可以按照下面的方式转换它。varmyClass=MyClass()varclassPtr=unsafeBitCast(myClass,UnsafePointer.self)和你的功能,funcsomeSwiftClass(voidPointer:UnsafeMutablePointer){}