我试过了
var timer = NSTimer()
timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)
但是,我说错了
'(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6' is not identical to 'NSTimer'
Run Code Online (Sandbox Code Playgroud) 如何将String转换为UInt8数组?
var str = "test"
var ar : [UInt8]
ar = str
Run Code Online (Sandbox Code Playgroud) 我正在尝试将数据从一个viewController推送到另一个.我已将viewController连接到另一个ViewController的navigationController,然后将标识符设置为"showItemSegue"
.我在这两行中出错:
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
Run Code Online (Sandbox Code Playgroud)
图示:
代码:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
nextId = itemArray?.objectAtIndex(indexPath.row).objectForKey("objectId") as? NSString
self.performSegueWithIdentifier("showItemSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showItemSegue") {
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
}
}
Run Code Online (Sandbox Code Playgroud) 例如,在python中,我可以说出类似的内容
arr = range(0,30)
Run Code Online (Sandbox Code Playgroud)
并获得具有所述元素的数组.我怀疑Swift中的下标可能有类似的东西,但在搜索文档和Apple的iBook后,我找不到用于生成所述阵列的"电池包含"解决方案.
这是我必须手动编写代码的东西,还是有预先编写的方法呢?
在最近的一个问题中,海报上有这个有趣的代码:
self.view.backgroundColor = .whiteColor()
Run Code Online (Sandbox Code Playgroud)
我很惊讶地看到这一点.我只见过用于枚举值的前导点符号.在这种情况下,backgroundColor
是类型UIColor?
并且whiteColor
是一个UIColor
返回a 的类方法UIColor
.
为什么这样做?这是调用工厂方法的合法方式吗?
我想通过将每个[String : Int]
字典显示为数组中的字符串,将我的字典转换为数组.
例如:
var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]
Run Code Online (Sandbox Code Playgroud)
我知道myDict.keys.array
并且myDict.values.array
,但我希望它们一起出现在阵列中.这就是我的意思:
var myDictConvertedToArray = ["attack 1", "defend 5", "block 12"]
Run Code Online (Sandbox Code Playgroud)
提前致谢.
在Swift中,我有一个使用programmaticaly创建的按钮:
var button = UIBarButtonItem(title: "Tableau", style: .Plain, target: self, action: "tabBarTableauClicked")
Run Code Online (Sandbox Code Playgroud)
我希望当用户单击该按钮时,它会更改viewControllers.这是以下代码tabBarTableauClicked
:
func tabBarTableauClicked(){
performSegueWithIdentifier("tableau", sender: self)
}
Run Code Online (Sandbox Code Playgroud)
但它显然不起作用,因为没有带有名为"tableau"的标识符的segue.
我无法使用Ctrl +单击按钮创建一个segue并拖动到第二个viewController,因为按钮是以编程方式创建的,而不是在Storyboard中创建的.
如何在Swift中以编程方式创建带有标识符的segue?
这在Xcode 8 beta 6中不再有效:
let colors:CFArray = [fromColor.cgColor, toColor.cgColor]
Run Code Online (Sandbox Code Playgroud)
要么
let gradient:CGGradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors:[fromColor.cgColor, toColor.cgColor], locations:[0.0, 1.0])!
Run Code Online (Sandbox Code Playgroud)
错误是:上下文类型'CFArray'不能与数组文字一起使用
从数组转换为CFArray的最新方法是什么?
[1, 2, 3, -1, -2].filter({ $0 > 0 }).count // => 3
[1, 2, 3, -1, -2].lazy.filter({ $0 > 0 }).count // => 3
Run Code Online (Sandbox Code Playgroud)
添加lazy
到第二个语句的优点是什么?根据我的理解,当使用lazy
变量时,内存在使用时被初始化为该变量.在这种情况下它是如何有意义的?
试图LazySequence
更详细地了解其用途.我曾使用过的map
,reduce
并且filter
功能上的序列,但从来没有对lazy
序列.需要了解为何使用此功能?
我有一个实例,UIBezierPath
我想将笔画的颜色改为黑色以外的东西.有谁知道如何在Swift中这样做?
swift ×10
ios ×4
arrays ×3
segue ×2
dictionary ×1
nstimer ×1
range ×1
storyboard ×1
swift3 ×1
uibezierpath ×1
uistoryboard ×1