标签: swift-playground

NSScanner scanString:intoString:不在游乐场工作

我正在尝试为som天气数据制作一个快速版本的xml解析器:

import Cocoa
import Foundation

let string2: NSString? = "cdefg jajaja <time> bbb"

let scanner = NSScanner(string: string2)
scanner.string
let searchString:NSString? = "<time"
var result: NSString?  = nil //= AutoreleasingUnsafePointer<NSString?>

scanner.charactersToBeSkipped = nil
scanner.scanString(searchString, intoString: nil) // this is not working (false)

scanner.scanUpToString( ">", intoString: &result) // this is working

result // result = "cdefg jajaja <time"
Run Code Online (Sandbox Code Playgroud)

为什么"scanner.scanString(searchString,intoString:nil)"返回nil?

swift swift-playground

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

枚举方法调用解析为"未使用的函数"

我一直在使用Xcode Playgrounds在Swift中乱搞.我知道Swift枚举比它们的Obj-C等价物强大得多.所以我想我将包含颜色的枚举作为成员值,并在枚举中添加一个方法来获取颜色的十六进制值.

但是,我收到错误 - "表达式解析为未使用的函数".我觉得这可能与让方法接受成员值作为参数有关,但我可能错了.代码如下.有人可以开导我吗?

enum Color {

case Red,Blue,Yellow

func hexValue (aColor: Color) ->  String { //Find hex value of a Color
    switch aColor {
    case .Red:
        return "#FF0000"
    case .Yellow:
        return "#FFFF00"
    case .Blue:
        return "#0000FF"
    default:
        return "???????"
    }
  }
}

Color.hexValue(Color.Red) //Error: "Expression resolves to an unused function"
Run Code Online (Sandbox Code Playgroud)

methods enums swift swift-playground

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

Swift关闭奇怪的行为

为什么这段代码编译(在Playground中):

func text (f: String -> Void) {
    f("text")
}

func foo(s: String) {
    countElements(s)
}

text() {
    s in
        foo(s)
}
Run Code Online (Sandbox Code Playgroud)

而这个不是:

func text (f: String -> Void) {
    f("text")
}

func foo(s: String) {
    countElements(s)
}

text() {
    s in
        countElements(s)
}
Run Code Online (Sandbox Code Playgroud)

有错误消息:

Cannot convert the expression's type '(($T3) -> ($T3) -> $T2) -> (($T3) -> $T2) -> $T2'       to type '_BuiltinIntegerLiteralConvertible'
Run Code Online (Sandbox Code Playgroud)

我可以说有返回类型的东西,有点Swift认为我想返回int但我只想打印它

xcode objective-c ios swift swift-playground

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

我对Swift和基本几何有什么问题

当我使用硬编码数据的直径和高度Swift运行模拟器很好,但当我尝试使用TextField.text.toInt()使用文本值,然后我不断收到非常恼人的错误消息: - 无法调用\ $ st15类型的参数列表

我哪里错了.我是Swift的新手,之前只使用过AppInventor来创建一个有几千次下载的应用程序.我是一个热情但可能很慢的学习者,但如果有人能帮助我一点点,我会到那儿.注意:公式只是PIr2 xh来给出汽缸容积.我想用直径来解释我每次减半的原因.

    let PI = 3.142

    var bodyDiameter = bodyDiameterTextField.text.toInt()  // 3.0
    var bodyHeight = bodyHeightTextField.text.toInt()   // 10.0
    var cylinderVolume: Double
    var cylinderVolume = (PI * (bodyDiameter / 2.0) * (bodyDiameter / 2.0)) * bodyHeight

    println("cylinderVolume")

    cylinderVolumeLabel.text = "(cylinderVolume)"
Run Code Online (Sandbox Code Playgroud)

以下是与Bodydiameter和Bodyheight的硬编码值相同的代码.这一切在游乐场和模拟器中都很棒.我猜它与Integers和Floats有关,但我可能会出局.

   let PI = 3.142

    var bodyDiameter = 3.0
    var bodyHeight = 10.0
    var cylinderVolume = (PI * (bodyDiameter / 2.0) * (bodyDiameter / 2.0)) * bodyHeight

    println("cylinderVolume")

    cylinderVolumeLabel.text = "(cylinderVolume)"
Run Code Online (Sandbox Code Playgroud)

nsstring ios swift swift-playground

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

NSTask在操场上

我想知道NSTask是否在游乐场不可用.这不编译

import Foundation
let task = NSTask()
Run Code Online (Sandbox Code Playgroud)

有谁知道这是否可能还是我错过了什么?

更新 可以创建iOS或OS X游乐场.也可以在最初创建游乐场后更改所选平台:Xcode> View> Utilities> Show File Inspector (cmd + opt + 1) - >平台有一个下拉菜单.

macos nstask swift swift-playground

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

Swift 2 NOT 按位运算未按预期运行

我正在尝试使用按位非运算符在 Swift 中翻转数字的所有位 ~

func binary(int: Int) -> String {
    return String(int, radix: 2)
}

let num = 0b11110000
binary(num) //prints "11110000"

let notNum = ~num
binary(notNum) //prints "-11110001"
Run Code Online (Sandbox Code Playgroud)

我的理解是notNum应该打印出00001111( docs ) 而不是打印出来-11110001。这里发生了什么?

bitwise-operators ios swift swift-playground swift2

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

如何将我自己的类导入Xcode 7游乐场?

我知道之前曾问过这个问题,但我没有找到与Xcode 7和Swift 2相关的答案.许多答案都是模棱两可,不正确或过时的.

是否有明确的方法将单个类或一组类导入游乐场?

swift swift-playground xcode7 swift2

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

在swift playground中导入pod时"没有这样的模块"

使用Xcode 7.2.1.

我正在按照本教程让pods在游乐场工作.我已将操场添加到工作区中,并且podfile正在链接到操场.

但导入仍然无效 - "没有这样的模块"

https://littlebitesofcocoa.com/138-using-cocoapods-in-xcode-playgrounds

//导入不起作用

在此输入图像描述

// Podfile
target 'spaceships' do
end

platform :ios, '9.0'
use_frameworks!
link_with 'spaceships', 'imports', 'test'
Run Code Online (Sandbox Code Playgroud)

https://github.com/mingyeow/ImportToPlaygroundDoesNotWork

xcode cocoapods swift-playground

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

打印角色在同一个游乐场swift 3

在同一行打印字符

for i in 1...5
  {
     print(i)
  }
Run Code Online (Sandbox Code Playgroud)

操作需要像

12345
Run Code Online (Sandbox Code Playgroud)

但是印刷品就像

1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)

任何人都有解决方案.

ios swift-playground swift3

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

无法在Xcode Playgrounds中初始化UICollectionView

我试图初始化UICollectionView:

import UIKit
import PlaygroundSupport

let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
Run Code Online (Sandbox Code Playgroud)

但是我得到的只是

error: Execution is interrupted, reason: signal SIGBART
Run Code Online (Sandbox Code Playgroud)

xcode uicollectionview swift swift-playground

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