小编ABa*_*ith的帖子

在Swift 2中使用split函数

假设我想用空格分割字符串.此代码段在Swift 1.x中正常工作.它在Xcode 7 Beta 1中的Swift 2中不起作用.

var str = "Hello Bob"
var foo = split(str) {$0 == " "}
Run Code Online (Sandbox Code Playgroud)

我得到以下编译器错误:

Cannot invoke 'split' with an argument list of type '(String, (_) -> _)
Run Code Online (Sandbox Code Playgroud)

有人知道怎么称呼这个吗?

更新:添加了一条说明,这是针对Xcode 7 beta 1的.

swift2

70
推荐指数
3
解决办法
4万
查看次数

找出字符串中的字符是否是表情符号?

我需要找出字符串中的字符是否是表情符号.

例如,我有这个角色:

let string = ""
let character = Array(string)[0]
Run Code Online (Sandbox Code Playgroud)

我需要弄清楚那个角色是不是表情符号.

string character ios emoji swift

68
推荐指数
7
解决办法
3万
查看次数

Swift中的泛型数组

我一直在玩不同类型的泛型类数组.用一些示例代码解释我的问题最容易:

// Obviously a very pointless protocol...
protocol MyProtocol {
    var value: Self { get }
}

extension Int   : MyProtocol {  var value: Int    { return self } }
extension Double: MyProtocol {  var value: Double { return self } }

class Container<T: MyProtocol> {
    var values: [T]

    init(_ values: T...) {
        self.values = values
    }

    func myMethod() -> [T] {
        return values
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试创建一个像这样的容器数组:

var containers: [Container<MyProtocol>] = []
Run Code Online (Sandbox Code Playgroud)

我收到错误:

协议"MyProtocol"只能用作通用约束,因为它具有Self或关联类型要求.

要解决这个问题,我可以使用[AnyObject]:

let containers: [AnyObject] …
Run Code Online (Sandbox Code Playgroud)

arrays generics swift swift-protocols

52
推荐指数
2
解决办法
3万
查看次数

"变量'xxx'从未发生变异;考虑改为'让'常数"错误

我有以下问题.我使用下面的代码,我得到了问题

"变量'特征'从未发生变异;考虑改为'让'常数"

for var characteristic:CBCharacteristic in service.characteristics ?? [] {
    print(str)
    _selectedPeripheral!.writeValue(str.dataUsingEncoding(NSUTF8StringEncoding)!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)
}
Run Code Online (Sandbox Code Playgroud)

当我改为"let"时,出现错误:

'let'模式不能嵌套在已经不可变的上下文中

为什么它会向我推荐更改,然后将其标记为错误?

xcode7 swift2

34
推荐指数
1
解决办法
4750
查看次数

如何扩展满足多个约束的协议 - Swift 2.0

我正在尝试扩展协议,以便它可以满足其他协议的多个约束.如何调整代码以使其正确?非常感谢.

extension Moveable where Self: Animal && Self: Aged {
    public var canMove: Bool { return true }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

protocols swift2

27
推荐指数
2
解决办法
9533
查看次数

Swift - UITableView滚动事件

我想知道如何检测是否UITableView滚动(向上或向下).我想在UITableView滚动时隐藏键盘self.view.endEditing(true).

提前致谢

events scroll hide uitableview swift

18
推荐指数
4
解决办法
2万
查看次数

以Swift为整数获取屏幕大小

为了在屏幕中随机定位元素但在边界内,我需要快速分辨率/屏幕尺寸.我已经知道如何使用CGGraphics获得它.不幸的是我需要计算一下,需要它作为类型UInt32,而不是CGFloat.我不能用那种方式进行铸造.有任何想法吗?

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
//What I want to do
let screenHeight = screenSize.height-arc4random_uniform(screenSize.height)
Run Code Online (Sandbox Code Playgroud)

ios swift

12
推荐指数
1
解决办法
3万
查看次数

SpriteKit中的正弦波运动

我想从屏幕的第一个点到屏幕的最后一个点进行正弦波运动,与屏幕的大小无关.

这是我的代码,但它无法正常工作:

 SKSpriteNode* RedBird = (SKSpriteNode*)[self childNodeWithName:@"RedBird"];
CGPoint currentPoint=CGPointMake(-60,0);
double width=self.frame.size.width/4;
CGPoint cp1=CGPointMake(width, self.frame.size.height/2);
CGPoint cp2=CGPointMake((width*3), 0);
CGPoint e=CGPointMake(self.frame.size.width+200, 0);


CGMutablePathRef cgpath = CGPathCreateMutable();


CGPathMoveToPoint(cgpath,NULL, currentPoint.x, currentPoint.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);



[RedBird runAction:[SKAction group:@[[SKAction repeatActionForever:RedBirdAnimation],[SKAction followPath:cgpath asOffset:YES orientToPath:NO duration:12.0]]]];


CGPathRelease(cgpath);
Run Code Online (Sandbox Code Playgroud)

ios sprite-kit

10
推荐指数
2
解决办法
2549
查看次数

自我协议的行为

我最近在Swift中阅读了Protocols,Generic Type Constraints和Arrays.我的问题涉及博客中的以下两个例子:

代码:

protocol MyProtocol1 {
    var myValue: Self { get }
}

let array: [MyProtocol1] = []  // Error.
Run Code Online (Sandbox Code Playgroud)

产生错误:

协议"MyProtocol1"只能用作通用约束,因为它具有Self或关联类型要求.

这是预期的,并且有关于该主题的几个SO问题.但是,通过更改myValue为函数,不再有任何错误,但在这两种情况下Self都会返回.

protocol MyProtocol2 {
    func myValue() -> Self
}

let array: [MyProtocol2] = []  // This is okay.
Run Code Online (Sandbox Code Playgroud)

有谁知道这个看似奇怪的行为的原因?

generics protocols swift

9
推荐指数
1
解决办法
395
查看次数

Swift 2.0中的泛型

我已经浏览了Apple开发者网站上的Swift教程,但我不理解泛型的概念.有人能够以简单的方式解释它吗?例如:

func swapTwoValues<T>(inout a: T, inout b: T) {
    let temporaryA = a
    a = b
    b = temporaryA
}
Run Code Online (Sandbox Code Playgroud)

generics ios swift swift2

9
推荐指数
2
解决办法
805
查看次数