在 Swift 中,如何创建一个包含捕获列表和参数的闭包?
我使用过以任何一种形式呈现的代码,但不知道如何创建具有参数和捕获列表的闭包。
例如
关闭参数列表:
myFunction {
(x: Int, y: Int) -> Int in
return x + y
}
Run Code Online (Sandbox Code Playgroud)
关闭捕获列表:
myFunction { [weak parent = self.parent] in print(parent!.title) }
Run Code Online (Sandbox Code Playgroud)
使用捕获列表的示例尝试:
class MyTest {
var value:Int = 3
func myFunction(f: (x:Int, y:Int) -> Int) {
print(f(x: self.value, y: 5))
}
func testFunction() {
myFunction {
[weak self] (x, y) in //<--- This won't work, how to specify weak self here?
print(self.value)
return self.value + y
}
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试制作类似于master password 的我自己的密码管理器应用程序,它使用一种算法来生成密码,因此它们不必存储在客户端计算机上或在线。
为了实现这一点,我决定使用使用CryptoSwift库的 ChaCha20 密码算法。这是我目前拥有的代码(OS X 应用程序):
import Cocoa
import CryptoSwift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
do
{
let UInt8String = "Test".utf8
print("UTF8 string: \(UInt8String)")
let UInt8Array = Array(UInt8String)
print("UTF8 array: \(UInt8Array)")
let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
print("Encrypted data: \(encrypted)")
} catch _ {
}
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
Run Code Online (Sandbox Code Playgroud)
我得到错误的那一行是 …
我有一个 feed,我想显示图像,如果用户没有图像,它会在 json 中显示 null。如果用户确实有图像,则将使用图像 url 设置该图像。
我想删除它,这样它就不会占用表格视图单元格中的空间。
我的代码是:
if imageStringFromJson == nil {
//how do i remove the imageview? <-- need help here
} else {
//set image from url in imageview
}
Run Code Online (Sandbox Code Playgroud) 我必须快速上传文档,我必须使用 iCloud 执行此操作,以便我可以使用 icloud 将文件上传到我的服务器
我在我的应用程序中使用 Uber Api。尝试预订行程时出现问题。我收到 httpStatus 400 错误。根据他们的文档,这可能是由于多种因素造成的。

我怎么能通过状态代码(总是“400”)知道给定类型的哪个描述导致它?
我现在的代码:
if let httpstatuscode = response as? NSHTTPURLResponse
{
if (error != nil)
{
print("Error : \(error?.localizedDescription)")
}
else if httpstatuscode.statusCode != 202 && httpstatuscode.statusCode != 409
{
print("Status Code\(httpstatuscode.statusCode)")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
CommonViewController.alertViewUI("Alert", message: "Internal Server Error!")
})
}
else if httpstatuscode.statusCode == 409
{
// self.getUberRideLive()
print("Status Code\(httpstatuscode.statusCode)")
CommonViewController.alertViewUI("Alert", message: "You are already on a ride!")
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个String- "Hello"
我怎么能把它String分成一个Array包含character这样的东西 - ["H", "e", "l", "l", "o"]
谢谢.
当我点击图片下方屏幕右侧的3个点时,我正在努力实现类似Instagram的功能.这是点击点之前的样子:
以下是它的样子:
显然我的滚动图片是一个UITableView,但我的问题是,当我点击3个点并且它带有一个选项列表的第二个视图时,这是另一个UITableView,还是你认为他们只是提出一个UIView的?
在这样的场景中使用的最佳选择是什么?
我对Swift比较新,想知道是否可以在Swift中设置一个等于另一个变量的变量.我有一个虚拟计数器变量,我想设置等于另一个变量,但它给了我一个错误.它似乎可以在Playground环境中使用,但不能在ViewController中使用.
import UIKit
class ViewController: UIViewController {
var count = 10.0
var count2 = count
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我已经按照本教程:
https://www.youtube.com/watch?v=OaiLpVG6Tlc
正确(或者我相信),但是我在发布时遇到了这些错误:
2016-09-07 20:36:05.520:<FIRInstanceID/WARNING>无法获取默认令牌Error Domain = com.firebase.iid Code = 6"(null)"
2016-09-07 20:36:15.743 customLogin [833 :] <FIRAnalytics/WARNING>无法获取InstanceID:Error Domain = com.firebase.iid Code = -34018"(null)"
2016-09-07 20:36:19.504:<FIRInstanceID/WARNING>无法获取默认令牌Error Domain = com.firebase.iid Code = 6"(null)"
2016-09-07 20:36:46.642:<FIRInstanceID/WARNING>无法获取默认令牌Error Domain = com.firebase.iid Code = 6"( null)"
2016-09-07 20:37:28.821:<FIRInstanceID/WARNING>无法获取默认令牌Error Domain = com.firebase.iid Code = 6"(null)"
2016-09-07 20:38:31.108 :<FIRInstanceID/WARNING>无法获取默认令牌Error Domain = com.firebase.iid Code = 6"(null)"
我怎样才能解决这个问题?
当我尝试按下我的应用程序上的登录按钮或创建帐户按钮时,我也收到此错误:
"访问钥匙串时发生错误".
let interestingNumbers = {
"Prime": [2,3,5,7,11,13],
"Fibonacci": [1,1,2,3,5,8],
"Square": [1,4,9,16,25],
}
var largest = 0
var largestKind = "Prime"
for (kind, numbers) in interestingNumbers {
var isThisKindLargest = false
for number in numbers{
if number > largest {
largest = number
isThisKindLargest = true
}
}
if isThisKindLargest {
largestKind = kind
}
}
print(largest)
print(largestKind)
Run Code Online (Sandbox Code Playgroud)
一行上的连续陈述必须用';'分隔 这个错误由Xcode-beta 7.1给出
有人能帮我把这段代码转换成 Swift 吗?
这里我提到了.h和.m在Objective-C 代码中。
Abc是一个UIViewController。我想在我的 Swift 代码中执行这个方法。S怎么可能?
Abc.h
+ (Abc*)sharedInstance;
- (void) startInView:(UIView *)view;
- (void) stop;
Run Code Online (Sandbox Code Playgroud)
Abc.m
static Abc*sharedInstance;
+ (Abc*)sharedInstance
{
@synchronized(self)
{
if (!sharedInstance)
{
sharedInstance = [[Abc alloc] init];
}
return sharedInstance;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud) 如果存在nil,强制解包会导致应用程序崩溃.这在您的应用程序的开发阶段非常酷.但是如果你懒得去做,那么这对于你的生产构建尤其令人头疼.
有没有人尝试过任何运算符重载/覆盖,以阻止生成构建的这些崩溃?
struct Physics {
static let smallCoin : UInt32 = 0x1 << 1
static let smallCoin2 : UInt32 = 0x1 << 2
static let ground : UInt32 = 0x1 << 3
}
Run Code Online (Sandbox Code Playgroud)
那是什么意思
UInt32 = 0x1 << 1?
和静态让?
swift2 ×13
swift ×8
ios ×7
arrays ×1
closures ×1
cryptoswift ×1
encryption ×1
firebase ×1
icloud ×1
objective-c ×1
optional ×1
parameters ×1
sprite-kit ×1
string ×1
struct ×1
swift2.3 ×1
swift3 ×1
uiimageview ×1
uitableview ×1
upload ×1
xcode7 ×1