我正在处理 swift 2.0 中的一个问题
我从 API 获取一个 json 文件,并尝试从中解开一些字符串。
有时这个 json 会给我一个带有场地街道名称的字符串,但有时不会。所以当我尝试这个时
var street = arrRes[indexPath.row]["venueLocation"]!["street"] as! String
Run Code Online (Sandbox Code Playgroud)
它总是让我的应用程序崩溃,说它为零。当我评论它时,我的应用程序运行完美,但它不显示街道。知道如何在没有任何问题的情况下解开字符串nil吗?
如果尝试过这段代码
var street = arrRes[indexPath.row]["venueLocation"]!["street"] as! String
if street == "" {
street = "n/a"
}
Run Code Online (Sandbox Code Playgroud)
但它也失败了。
我正在使用 Alamofire 从网络服务获取图像。我使用以下代码在 UICollectionView 的自定义单元格中显示这些图像
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let reuseIdentifier = "cell"
// get a reference to our storyboard cell
let cell : ImageCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellCollection", forIndexPath: indexPath) as! ImageCollectionCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.backgroundColor = UIColor.yellowColor() // make cell more visible in our example project
let imageGalleryURLString = self.MutableArray.objectAtIndex(indexPath.row) .valueForKey("thumbnail") as! String
let imageURL = NSURL(string:imageGalleryURLString)
cell.imageviewCollectionCell.sd_setImageWithURL(imageURL)
return cell …Run Code Online (Sandbox Code Playgroud) 我正在使用 录制视频AVFoundation,并从视频创建了一个预览,当我完成录制视频时,视频正在预览中播放,但是当我使用以下方法保存视频时:
var fileName = "\(self.tField.text!).mp4"
fileName = fileName.stringByReplacingOccurrencesOfString(" ", withString: "_")
let path = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
//let originVideoFile = self.filePath
let destinationVideoFile = path.URLByAppendingPathComponent(fileName)
let data = NSData(contentsOfURL: self.filePath)
//try data!.writeToURL(destinationVideoFile!, options: [])
let fileManager = NSFileManager()
fileManager.createFileAtPath((destinationVideoFile?.path)!, contents: data, attributes: nil)
Run Code Online (Sandbox Code Playgroud)
视频文件已创建,但当我想重新播放视频时,我无权访问此文件,但相机胶卷可以播放视频。
要获得“ filePath”,我正在使用以下委托方法AVFoundation:
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
print("capture did finish")
print(captureOutput);
print(outputFileURL);
self.filePath = outputFileURL
performSegueWithIdentifier("previewVideo", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, …Run Code Online (Sandbox Code Playgroud) 我正面临一个恼人的问题,我希望你们能帮我解决这个问题。
我有两个协议和一个类:
protocol Prot1: AnyObject {
}
protocol Prot2 {
associatedtype T: AnyObject
}
class TheClass: Prot2 {
typealias T = Prot1
}
Run Code Online (Sandbox Code Playgroud)
这导致编译器说:
在导航中,它显示了更多详细信息:
我真的需要关联类型是AnyObject类型。这就是我需要帮助的原因。
有谁知道如何解决这个问题?
非常感谢。
OBS:我使用的是 swift 2.3
从这个教程我看到了这种"!" +自我操作,它在Swift中究竟意味着什么?
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled())
self.transitionContext?.viewControllerForKey(UITransitionContextFromViewControllerKey)?.view.layer.mask = nil
}
!self.transitionContext!.transitionWasCancelled() //returns true
self.transitionContext!.transitionWasCancelled() // without ! , return false
Run Code Online (Sandbox Code Playgroud) 我正在使用Swift2,我有这两个循环:
for var i: Int = 0; i < 4; i++ {
print("the i = \(i)")
}
and
for var i: Int = 0; i < 4; ++i {
print("the i = \(i)")
}
Run Code Online (Sandbox Code Playgroud)
我在两者中都得到了完全相同的印刷品.告诉我为什么?
正在寻找与Swift 2和iOS 9联网的有用教程,但似乎该主题没有在线内容.我已经看过WWDC会话网络与NSURLSession但我无法调整iOS9的新API以获取和解析JSON数据的异步方式.我尝试过的:
do {
if let url = NSURL(string: "site/api.php?option=fetchOps"),
let data = NSData(contentsOfURL: url),
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String:AnyObject]] {
}
} catch let error as NSError {
print(error.description)
}
Run Code Online (Sandbox Code Playgroud)
请分享您在iOS9和Swift2上的网络体验,任何最佳实践可能是Singleton类的网络和解析可能是一个ORM?
我在GameScene中创建了一个得分变量.现在我想在另一个场景中看到结果,例如GameOverScene.我怎么能这样做?
这听起来很简单,我知道我将被告知仅获取url会话的内容。但是我特别想知道源代码是否可以从浏览器的最终呈现中获取(后javascript / JSON格式)
编辑:我在获取帖子已处理内容时遇到问题。当我使用swift / objective-c或Java下载URL的内容时,内容字符串具有基于Json搜索首选项的搜索结果的占位符。但是,在浏览器中,右键单击并选择“查看源代码”将显示完整的结果页面。我需要一种以编程方式获取此js后处理内容的方法。
编辑:我已经在webView中加载了代码。这是我的代码:
func loadAddressURL() {
let url = "https://www.adomain.com/apath"
let URL = NSURL(string: url)!
let request = NSURLRequest(URL: URL)
webView.mainFrame.loadRequest(request)
}
Run Code Online (Sandbox Code Playgroud) 我尝试使用drand48()创建一个随机获取值的变量, 但在每次运行中,我的变量采用相同的值.任何人都可以帮助我,(我想在每次运行中我的变量取不同的价值)???
let number = drand48 ()
Run Code Online (Sandbox Code Playgroud)