我想用SwiftJSON处理json,但我堆叠了.有没有人给我看示例代码?
我试图使用这个库. https://github.com/SwiftyJSON/SwiftyJSON
虽然我在同一个项目中放置了SwiftyJSON.swift,但我有错误"没有这样的模块"SwiftyJSON""所以纠正我的代码或者向我展示使用swiftyJSON lib从web处理json的示例代码.
这是我的代码:
import UIKit
import SwiftyJSON // No such module "SwiftyJSON"
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures")
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
var hoge = JSON(data)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources …Run Code Online (Sandbox Code Playgroud) 我发现以下代码编译和工作:
func foo(p:UnsafePointer<UInt8>) {
var p = p
for p; p.memory != 0; p++ {
print(String(format:"%2X", p.memory))
}
}
let str:String = "??"
foo(str)
Run Code Online (Sandbox Code Playgroud)
这打印E4BB8AE697A5,这是一个有效的UTF8表示??
据我所知,这是无证行为.来自文件:
当函数声明为采用UnsafePointer参数时,它可以接受以下任何一种:
- nil,作为空指针传递
- UnsafePointer,UnsafeMutablePointer或AutoreleasingUnsafeMutablePointer值,必要时转换为UnsafePointer
- 一个输入输出表达式,其操作数是类型为Type的左值,它作为左值的地址传递
- 一个[Type]值,作为指向数组开头的指针传递,并在调用期间延长生命周期
在这种情况下,str不属于他们.
我错过了什么吗?
添加:
如果参数类型是,则它不起作用 UnsafePointer<UInt16>
func foo(p:UnsafePointer<UInt16>) {
var p = p
for p; p.memory != 0; p++ {
print(String(format:"%4X", p.memory))
}
}
let str:String = "??"
foo(str)
// ^ 'String' is not convertible to 'UnsafePointer<UInt16>'
Run Code Online (Sandbox Code Playgroud)
即使内部String表示是UTF16
let str = "??" …Run Code Online (Sandbox Code Playgroud) 我正在为PC浏览器开发音频流媒体网络服务.
我们希望使用rtmpt(e)协议进行Flash插件流式传输.
主HTML页面包含https://URL.
问题是,当我们的SWF尝试连接流服务器(通过HTTP隧道)时,某些浏览器(即Chrome)会在URL栏中的安全图标上显示警告:
您与???.???.com的连接是使用128位加密进行加密的.但是,此页面包含其他不安全的资源.其他人在传输过程中可以查看这些资源,攻击者可以修改这些资源以更改页面外观.
并在开发者控制台上:
The page at 'https://***.***.com/' was loaded over HTTPS, but displayed insecure content from 'http://stream.***.net/fcs/ident2': this content should also be loaded over HTTPS.
The page at 'https://***.***.com/' was loaded over HTTPS, but displayed insecure content from 'http://***.***.***.113/open/1': this content should also be loaded over HTTPS.
...
Run Code Online (Sandbox Code Playgroud)
我认为这是因为Flash在访问HTTP时使用了浏览器的URL加载工具.
我该如何避免这些警告?
我们不想使用,rtmp(e)因为1935可能会被防火墙阻止在用户环境中,也不会rtmps因为我们的流媒体服务器不支持它.
我们不希望http://因为要求而使用主HTML.
我可以用iOS SDK运行Swift REPL吗?
我想UIKit在REPL中导入和使用,但没有成功.
$ xcrun --sdk iphonesimulator8.1 --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk
$ xcrun --sdk iphonesimulator8.1 swift
Welcome to Swift! Type :help for assistance.
1> import UIKit
/var/folders/kb/xgglxb597sv6h8b744d5vft00000gn/T/lldb/92014/repl1.swift:2:8: error: no such module 'UIKit'
import UIKit
^
$ swift -sdk `xcrun --sdk iphonesimulator8.1 --show-sdk-path`
Welcome to Swift! Type :help for assistance.
1> import UIKit
/var/folders/kb/xgglxb597sv6h8b744d5vft00000gn/T/lldb/91881/repl1.swift:2:8: error: no such module 'UIKit'
import UIKit
^
1> import Cocoa
2>
Run Code Online (Sandbox Code Playgroud)
我正在使用Xcode版本6.1(6A1052d)
我正在尝试在Swift中创建一个iOS应用程序,它要求我在一行的第一个冒号(:)中拆分一行文本.我已经尝试在字符串上使用componentsSeparatedByString方法,但除了收到错误声明"'NSString'没有名为'componentSeparatedByString'的成员"之外,该方法实际上并没有按我的意愿行事.
对于更多的上下文,我试图在游戏中分离一条线,即形式
<character name> : <line>
Run Code Online (Sandbox Code Playgroud)
进入角色的名字和线条.我假设角色的名字中没有冒号,所以我想在该行的第一个冒号处拆分,这样如果文本中有任何冒号,我就不会将该行的文本拆分成碎片.
所以,我的第一个问题:为什么不使用'componentsSeparatedByString'在以下代码中有效:(以下代码不是我之前提到过的情况,但它是我想要修复并保留的东西,所以我想使用它而不是冒号破坏代码.)
var fileRoot = NSBundle.mainBundle().pathForResource("LonelyImpulse", ofType: "txt")
var contents = NSString(contentsOfFile:fileRoot!, encoding: NSUTF8StringEncoding, error: nil)!
let stringArray = contents.componentSeparatedByString("\n")
Run Code Online (Sandbox Code Playgroud)
而我的第二个:我怎样才能在冒号(:)字符的第一个匹配中拆分字符串,而不是沿着所有匹配?
我正在制作一个在滚动视图中有(子)视图的应用程序.我知道我必须明确设置子视图的宽度,因为滚动视图不会向孩子提供该信息,但是如何根据设备的宽度使该子视图的宽度自动调整?
例如,在下面我将宽度设置为380,它将iPhone 6中的"Quotes"作为中心,但是它们在iPhone 5中略微偏向中心.

Optional<T>有map方法.
/// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.
func map<U>(f: (T) -> U) -> U?
Run Code Online (Sandbox Code Playgroud)
当我们想要转换Int?为UInt64?,我们可以:
let iVal:Int? = 42
let i64Val = iVal.map { UInt64($0) }
Run Code Online (Sandbox Code Playgroud)
代替:
var i64Val:UInt64?
if let iVal = iVal {
i64Val = UInt64(iVal)
}
Run Code Online (Sandbox Code Playgroud)
在这里,ImplicitlyUnwrappedOptional<T>有相同的方法:
/// If `self == nil`, returns `nil`. Otherwise, returns `f(self!)`.
func map<U>(f: (T) -> U) -> U!
Run Code Online (Sandbox Code Playgroud)
所以我试过......但失败了:(
let iVal:Int! = 42
let i64Val = iVal.map { …Run Code Online (Sandbox Code Playgroud) 根据View Controller Programming Guide,我们可以通过分配to来显式卸载self.viewfrom 。UIViewControllernilself.view
但是在 Swift 中,view属性 inUIViewController被声明为
var view: UIView
Run Code Online (Sandbox Code Playgroud)
它不是UIView!,因此以下代码不会编译
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
if self.view.window == nil {
self.view = nil
// ^ Type 'UIView' does not conform to protocol 'NilLiteralConvertible'
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以在 Swift 中做到这一点?
我正在制作一个我需要我的spriteNodes碰撞的游戏.我按照教程,但它不适合我.
当我运行我的游戏并且两个对象发生碰撞时,输出监视器中没有println("beginContact"),因此不会调用didBeginContact函数.
override init(size:CGSize) {
super.init(size: size)
player.planeSprite = SKSpriteNode(imageNamed: "plane5")
player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size)
player.planeSprite.physicsBody?.dynamic = true;
player.planeSprite.physicsBody?.categoryBitMask = playerCategory;
player.planeSprite.physicsBody?.contactTestBitMask = noteCategory;
player.planeSprite.physicsBody?.collisionBitMask = 0;
player.planeSprite.physicsBody?.usesPreciseCollisionDetection = true;
player.planeSprite.position = CGPointMake(player.legalPositions[1], player.planeSprite.size.height/2)
self.addChild(player.planeSprite)
}
func addNote() {
var note:SKSpriteNode = SKSpriteNode(imageNamed: "note")
note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size)
note.physicsBody?.dynamic = true
note.physicsBody?.categoryBitMask = noteCategory
note.physicsBody?.contactTestBitMask = playerCategory
note.physicsBody?.collisionBitMask = 0
let minX = note.size.width/2
let maxX = self.frame.size.width - note.size.width/2
let rangeX = maxX - minX
let position = Int(arc4random_uniform(3)) …Run Code Online (Sandbox Code Playgroud) 我已经阅读过类似的问题/答案,但没有一个能解决我的问题。
我有一个这样的对象
class Chat {
unreadMessages: Int = 0
messages: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
然后我有一组 Chat 对象,我需要按多个条件对其进行排序,首先通过与 grater 未读消息聊天,然后与 grater total 消息聊天。
埃斯。
Obj TotMes Unread
Chat A 10 3
Chat B 1 0
Chat C 4 0
Chat D 9 9
Run Code Online (Sandbox Code Playgroud)
所需的输出:
Chat D 9 9
Chat A 10 3
Chat C 4 0
Chat B 1 0
Run Code Online (Sandbox Code Playgroud)
我尝试使用这种 alhoritm,但似乎不起作用:
let sorted = chats.sort({ (c1, c2) -> Bool in
if c1.unreadMessages > c2.unreadMessages {
return true
}
if c1.messages …Run Code Online (Sandbox Code Playgroud)