假设我想用空格分割字符串.此代码段在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的.
我需要找出字符串中的字符是否是表情符号.
例如,我有这个角色:
let string = ""
let character = Array(string)[0]
Run Code Online (Sandbox Code Playgroud)
我需要弄清楚那个角色是不是表情符号.
我一直在玩不同类型的泛型类数组.用一些示例代码解释我的问题最容易:
// 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) 我有以下问题.我使用下面的代码,我得到了问题
"变量'特征'从未发生变异;考虑改为'让'常数"
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'模式不能嵌套在已经不可变的上下文中
为什么它会向我推荐更改,然后将其标记为错误?
我正在尝试扩展协议,以便它可以满足其他协议的多个约束.如何调整代码以使其正确?非常感谢.
extension Moveable where Self: Animal && Self: Aged {
public var canMove: Bool { return true }
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何检测是否UITableView
滚动(向上或向下).我想在UITableView
滚动时隐藏键盘self.view.endEditing(true)
.
提前致谢
为了在屏幕中随机定位元素但在边界内,我需要快速分辨率/屏幕尺寸.我已经知道如何使用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) 我想从屏幕的第一个点到屏幕的最后一个点进行正弦波运动,与屏幕的大小无关.
这是我的代码,但它无法正常工作:
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) 我最近在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)
有谁知道这个看似奇怪的行为的原因?
我已经浏览了Apple开发者网站上的Swift教程,但我不理解泛型的概念.有人能够以简单的方式解释它吗?例如:
func swapTwoValues<T>(inout a: T, inout b: T) {
let temporaryA = a
a = b
b = temporaryA
}
Run Code Online (Sandbox Code Playgroud)