使用新的Swift 1.2更新到Xcode 6.3.1,将旧方法countElement更改为count,但是当我切换到使用时count,它总是抛出此错误消息:
无法使用类型'(String)'的参数列表调用'count'
这个片段是我从Apple doc复制的,但仍然无法正常工作.
func printAndCount(stringToPrint: String) -> Int {
println(stringToPrint)
return count(stringToPrint)
}
func printWithoutCounting(stringToPrint: String) {
printAndCount(stringToPrint)
}
printAndCount("hello, world")
Run Code Online (Sandbox Code Playgroud) 我试过这个通过使用uicollectionview的扩展来获取集合视图中心的单元格的索引,但我总是得到nil而不是索引.我怎样才能解决这个问题?
extension UICollectionView {
var centerPoint : CGPoint {
get {
return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
}
}
var centerCellIndexPath: NSIndexPath? {
if let centerIndexPath: NSIndexPath = self.indexPathForItemAtPoint(self.centerPoint) {
return centerIndexPath
}
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
那么:在随机方法的UIViewController中我有这个:
if let centerCellIndexPath: NSIndexPath = collectionTemp!.centerCellIndexPath {
print(centerCellIndexPath)
} else {
println("nil")
}
Run Code Online (Sandbox Code Playgroud)
索引路径总是为零,我不明白为什么,因为单元格按顺序显示,除此之外一切都很好.

我正在尝试使用 UICollectionViews 获得七个水平可滚动按钮栏。我在启动和运行一个按钮栏时没有问题,但是当我使用多个集合视图时,我遇到了应用程序崩溃错误。有没有人知道如何在 Swift 中实现这一点或知道任何教程?我的第一个集合视图的代码在这里:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var tableImages: [String] = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png"]
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tableImages.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
cell.expansionCell.image = UIImage(named: tableImages[indexPath.row])
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("cell \(indexPath.row) selected")
}
}
Run Code Online (Sandbox Code Playgroud)
请帮忙!
我需要比较两个UIColors,但由于某种原因它总是返回false.我尝试比较使用==和.isEqual(),但它们似乎都不起作用.
//This is a sample of the colors I have created
let blue_color = UIColor(red: 122/255, green: 180/255, blue: 190/255, alpha: 1)
//This is the SpriteNode I have to compare
let square = SKSpriteNode(color: randomColorController(), size: ksquaresize)
Run Code Online (Sandbox Code Playgroud)
randomColorController()只是一个随机化颜色并返回它的函数,因此在创建square时会调用它.
func randomColorController() -> UIColor {
let random = arc4random_uniform(3) + 1
switch random {
case 1:
let color = blue_color
return color
case 2:
let color = yellow_color
return color
case 3:
let color = yellow_color
return color …Run Code Online (Sandbox Code Playgroud) 我在Swift中有这段代码:
var password = "Meet me in St. Louis"
for character in password {
if character == "e" {
print("found an e!")
} else {
}
}
Run Code Online (Sandbox Code Playgroud)
抛出以下错误:value of type 'String' has no member 'Generator' in Swift在行:for character in password
我试图在网上找到可能的错误,但我不能(加上我是Swift的新手并试图通过该语言的特性进行自我导航).
任何帮助将不胜感激(如果可能的话,加上我错过的简要说明)
我是Swift的新手,但是Swift语言出现了问题。
我应该做一个函数,创建一个二维矩阵NxN并找到对角线放置的元素的总和。我在用随机值填充数组时遇到了一些问题。
那是我的代码:
import Foundation
func diagonal (N:Int) {
var array: [[Int]] = [[0],[0]]
for row in 0...N {
for col in 0...N {
var z = Int(arc4random_uniform(100))
array[row][col] = z
}
}
println (array)
}
Run Code Online (Sandbox Code Playgroud)
没用
因此,期待您的帮助。
我正在使用enumerateChildNodesWithName删除和添加节点.我想知道是否有一种方法可以使用带有多个名称的enumerateChildNodesWithName.例如,目前我正在使用以下内容:
nodeBase.enumerateChildNodesWithName("ground", usingBlock: {
node, stop in
if node.position.x + positionX < -self.frame.size.width/2 - sizeSegmentWidth/2 {
node.removeFromParent()
}
})
nodeBase.enumerateChildNodesWithName("obstacle", usingBlock: {
node, stop in
if node.position.x + positionX < -self.frame.size.width/2 - sizeSegmentWidth/2 {
node.removeFromParent()
}
})
Run Code Online (Sandbox Code Playgroud)
但我希望做的是这样的事情(这不起作用,只是我正在尝试做的一个例子):
nodeBase.enumerateChildNodesWithName("ground" || "obstacle", usingBlock: {
node, stop in
if node.position.x + positionX < -self.frame.size.width/2 - sizeSegmentWidth/2 {
node.removeFromParent()
}
})
Run Code Online (Sandbox Code Playgroud) 是否可以在swift中子类化通用结构?
假设我们有一个结构:
struct Foo<T> {}
Run Code Online (Sandbox Code Playgroud)
我wana"子类"它添加一些功能:
struct Something {}
struct Bar<F>: Foo<Something> { /*<<<= ERROR: Inheritance from non-protocol type 'Foo<Something>' */
let store: Something
let someF: F
}
Run Code Online (Sandbox Code Playgroud)
如果用class替换struct,则此示例有效.
class Foo<T> {}
//struct Bar1<T>: Foo<T> { /* Inheritance from non-protocol type 'Foo<T>' */
// let name = "Bar"
//}
class Something {}
class Bar<F>: Foo<Something> { /* Inheritance from non-protocol type 'Foo<Something>' */
let store: F?
init(value: F?) {
self.store = value
}
}
Run Code Online (Sandbox Code Playgroud)
知道如何使这个结构工作吗?
我试图坚持价值类型,但迅速使其变得困难.
我正在使用带有swift的spritekit,我只是想尝试呈现一个新的场景......但是有些东西是错误的,并且会抛出错误.我很确定这是正确的语法,这真的让我陷入了一个循环.
这条线
let skView = self.view as SKView
Run Code Online (Sandbox Code Playgroud)
给我错误"SKView?不能转换为SKView
任何建议表示赞赏!*我目前的代码如下:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches{
let location = touch.locationInNode(self)
if(self.nodeAtPoint(location) == self.playButton)
{
var scene = PlayScene(size: self.size)
let skView = self.view as SKView
// Configure the view.
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想在一个语句中检查一个对象是否是一个类型NSNumber和一个布尔变量true.为此我写了如下:
let someBool = ...
if value.isKindOfClass(NSDictionary) {
// do something with dict
}
else if (let number = value as? NSNumber) && someBool{
//Do something with number
}
else {
// do something here
}
Run Code Online (Sandbox Code Playgroud)
但是,像'模式变量绑定这样的抛出错误不能出现在表达式中'.
如何在单一if条件下使用可选的展开和布尔条件?
swift ×10
ios ×5
sprite-kit ×3
arrays ×1
boolean ×1
for-loop ×1
generics ×1
iphone ×1
nodes ×1
nsindexpath ×1
optional ×1
removechild ×1
swift2 ×1
uicolor ×1
value-type ×1
xcode ×1
xcode6.3 ×1