所以我想拥有的是一个可以在函数中传递给它的闭包的类,它也可能在某个时候想要忽略那个闭包.如何检查闭包变量是否已设置,我可以在完成后删除它吗?
无法使用类型'的参数列表调用'!='(@lvalue(sucsess:Bool!,products:[AnyObject]!) - >()?,NilLiteralConvertible)'Type'(sucsess:Bool!,产品:[AnyObject] ]!) - >()?' 不符合协议'NilLiteralConvertible'
class someClass{
//typealias completionHandlerClosureType = (sucsess:Bool!, items:[AnyObject]!)->()
var completionHandler:(sucsess:Bool!, items:[AnyObject]!)->()?
var hitpoints = 100
var someset = ["oh no!","avenge me!"]
init(){}
func getHitFunc(impact:Int, passedCompletionsHandler:(sucsess:Bool!, items:[AnyObject]!)->()){
completionHandler = passedCompletionsHandler
hitpoints = hitpoints - impact
}
func checkIfDead{
if hitpoints<=0 { // The error received
if completionHandler != nil{// Cannot invoke '!=' with an argument list of type
//'(@lvalue (sucsess: Bool!, products: [AnyObject]!) -> ()?, NilLiteralConvertible)'
//run the handler if dead
completionHandler(sucsess: true, items: …Run Code Online (Sandbox Code Playgroud) 我一直在使用SKLightNodeSpriteKit(iOS8中的新功能)进行实验,即使是在一个非常简单的测试用例中,我的性能也非常糟糕.例如,在纯色的单个光源上,我在第3代iPad上获得13.2 FPS.如果我添加第二个光源,它会下降到一个非常糟糕的7.7 FPS.SKSpriteNode
WWDC会话视频SpriteKit中的新功能提到如果你在同一个精灵上有多个灯光,你可能会得到低于60 FPS,但我甚至不能得到60 FPS.这是相关部分.
这是我在swift中的测试场景(它从一个光源开始,可以通过点击添加更多):
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let center = CGPointMake(size.width / 2.0, size.height / 2.0)
let background = SKSpriteNode(color: SKColor.lightGrayColor(), size: size)
background.position = center
background.lightingBitMask = 1
addChild(background)
let light = SKLightNode()
light.position = center
light.falloff = 1.0
light.lightColor = SKColor(hue: 0.62 , saturation: 0.89, brightness: 1.0, alpha: 0.4) …Run Code Online (Sandbox Code Playgroud) enum Tree{
case Leaf(String)
case Node(Tree)
} //compiler not happy!!
enum Tree{
case Leaf(String)
case Node([Tree])
} //compiler is happy in (arguably) a more complex recursive scenario?
Run Code Online (Sandbox Code Playgroud)
Swift编译器如何为第二个(更复杂的)场景工作而不是第一个?
重写UITableViewDelegate的tableView:cellForRowAtIndexPath:导致以下编译错误:
使用选择器'tableView:cellForRowAtIndexPath:'的覆盖方法具有不兼容的类型'(UITableView!,NSIndexPath!) - > UITableViewCell!'
覆盖函数的当前实现是:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdent", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
return cell
}
Run Code Online (Sandbox Code Playgroud)
导致错误的原因是什么?
我无法弄清楚如何替换字符串中的所有特殊字符并将其转换为我可以在URL中使用的字符串.
我正在使用它:我上传一个图像,将其转换为base64,然后将其传递给Laravel框架,但是base64 string可以包含+, /, \等等,这会改变URL的含义.
我可以用+以下代码替换标志:
let withoutPlus = image.stringByReplacingCharactersInRange("+", withString: "%2B")
Run Code Online (Sandbox Code Playgroud)
然而,我不能用它作为NSString尝试和改变其他角色.
当然有一种方法可以只针对每一个特殊字符并将其转换为URL中可用的东西吗?
当尝试使用+=运算符追加a Character时String,我收到以下错误:
字符串与UInt8不同
错误发生在puzzleOutput += char下面代码中的行上:
let puzzleInput = "great minds think alike "
var puzzleOutput = ""
for char in puzzleInput
{
switch char
{
case "a","e","i","o","u":
continue
default:
puzzleOutput += char
}
}
println(puzzleOutput)
Run Code Online (Sandbox Code Playgroud)
我怎样才能追加Charater到String?
在我的项目中,我需要将数据发送到服务器,为此我使用以下代码来完成任务:
- (void)sendJSONToServer:(NSString *) jsonString
{
// Create a new NSOperationQueue instance.
operationQueue = [NSOperationQueue new];
//
// Create a new NSOperation object using the NSInvocationOperation subclass to run the operationQueueTask method
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(operationQueueTask:)
object:jsonString];
// Add the operation to the queue and let it to be executed.
[operationQueue addOperation:operation];
}//End of sendJSONToServer method
-(void) operationQueueTask:(NSString *) jsonString
{
//NSOperationQueue *remoteResultQueue = [[NSOperationQueue alloc] init];
dispatch_queue_t myQueue = dispatch_queue_create("SERVER_QUEUE",NULL);
dispatch_async(myQueue, ^{
// Performing long running …Run Code Online (Sandbox Code Playgroud) 嗨,我有两个阵列.
var firstArray = ["Hi", "Hello", "Mother"]
var secondArray = ["Yo", "Yee", "Father"]
Run Code Online (Sandbox Code Playgroud)
现在说我做了一个println("我看到约翰尼并说嗨")但它真的会用我的第二个字符串对象取代它,即"哟"
所以几乎用第二个数组替换第一个数组中的所有内容,就像他们在第一个数组中输入任何文字字符串的时候一样?我试图迅速做到这一点.我尝试使用stringByReplacingOccurrencesOfString循环遍历第一个数组,但我不确定如何在其中实现NSArray.有帮助吗?
如果我在下面做了怎么办?
var myString = "Hello this is a test."
var myDictionary = ["Hello":"Yo"]
for (originalWord, newWord) in myDictionary {
let newString = aString.stringByReplacingOccurrencesOfDictionary(myString, withString:newWord, options: NSStringCompareOptions.LiteralSearch, range: nil)
}
Run Code Online (Sandbox Code Playgroud)
我仍然无法弄清楚如果我把它放到println中("你好,你好吗?)如果每次用"yo"输入println语句时它会自动知道替换"hello"
swift ×7
ios ×3
ios8 ×2
block ×1
closures ×1
iphone ×1
nsoperation ×1
sklightnode ×1
sprite-kit ×1