Tag: 迅速

如何在swift中获得2个数组的常用元素列表

我有两个数组: fruitsArray = ["apple", "mango", "blueberry", "orange"] vegArray = ["tomato", "potato", "mango", "blueberry"] 我怎样才能得到这两个数组中的常用项目列表 ouptput = ["mango", "blueberry"] 我不能使用, if contains(array, string)因为我想比较2个数组。 请帮忙

如何从文件扩展名中拆分文件名在Swift中?

鉴于包中文件的名称,我想加载到我的Swift应用程序的文件。 所以我需要使用这个方法: let soundURL = NSBundle.mainBundle().URLForResource(fname, withExtension: ext) 无论出于何种原因,该方法需要从文件扩展名中分离出来的文件名。 很好,在大多数语言中很容易将两者分开。 但是到目前为止,我并没有发现它在Swift中是如此。 所以这里是我所拥有的: var rt: String.Index = fileName.rangeOfString(".", options:NSStringCompareOptions.BackwardsSearch) var fname: String = fileName .substringToIndex(rt) var ext = fileName.substringFromIndex(rt) 如果我不在第一行包括打字,我会在后面两行发现错误。 有了它,我得到了第一行的错误: Cannot convert the expression's type '(UnicodeScalarLiteralConvertible, options: NSStringCompareOptions)' to type 'UnicodeScalarLiteralConvertible' 我怎样才能从扩展名分割文件名? 有没有一些优雅的方式来做到这一点? 我对Swift感到兴奋,因为它好像比Objective C更优雅的语言。但现在我发现它有自己的麻烦。 第二次尝试:我决定做我自己的stringsearch方法: func rfind(haystack: String, needle: Character) -> Int { var […]

Swift – 使用downloadTaskWithURL下载video

我正在下载一个video感谢downloadTaskWithURL,我保存到我的画廊与此代码: func saveVideoBis(fileStringURL:String){ print("saveVideoBis"); let url = NSURL(string: fileStringURL); (NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location:NSURL?, r:NSURLResponse?, e:NSError?) -> Void in let mgr = NSFileManager.defaultManager() let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]; print(documentsPath); let destination = NSURL(string: NSString(format: "%@/%@", documentsPath, url!.lastPathComponent!) as String); print(destination); try? mgr.moveItemAtPath(location!.path!, toPath: destination!.path!) PHPhotoLibrary.requestAuthorization({ (a:PHAuthorizationStatus) -> Void in PHPhotoLibrary.sharedPhotoLibrary().performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(destination!); }) { completed, error in […]

iOS无法识别的select器发送到Swift中的实例

我有问题试图让用户按下它的UIButton工作。 我不断收到一个错误说:无法识别的select器发送到实例 override func viewDidLoad() { super.viewDidLoad() button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside) button.setTitle("Print", forState: UIControlState.Normal) button.font = UIFont(name: "Avenir Next", size: 14) button.backgroundColor = UIColor.lightGrayColor() self.view.addSubview(button) } func buttonClick(Sender: UIButton!) { myLabelInfo.text = "Hello" } 对于诸如func buttonClick(Sender: UIButton)的Swift方法,要传递给select器的addTarget方法的正确string是什么? 是“buttonClick”,“buttonClick:”,“buttonClickSender:”还是别的?

Firebase – 删除并重新安装应用程序不会取消对用户的身份validation

用下面的代码validation用户后(下面是我的代码的修剪版本,所以只有成功的login逻辑显示)… let firebaseReference = Firebase(url: "https://MY-FIREBASE.firebaseio.com") FBSession.openActiveSessionWithReadPermissions(["public_profile", "user_friends"], allowLoginUI: true, completionHandler: { session, state, error in if state == FBSessionState.Open { let accessToken = session.accessTokenData.accessToken firebaseReference.authWithOAuthProvider("facebook", token: accessToken, withCompletionBlock: { error, authData in if error != nil { // Login failed. } else { // Logged in! println("Logged in! \(authData)") } }) } }) } […]

在Swift(2.0)中正确处理NSJSONSerialization(try catch)?

arowmy init在Swift 2中工作正常,但是在Swift 2中,我从Xcode Call can throw, but it is not marked with 'try' and the error is not handled得到一个错误消息, Call can throw, but it is not marked with 'try' and the error is not handled在Call can throw, but it is not marked with 'try' and the error is not handled的let anyObj = NSJSONSerialization.JSONObjectWithData(data, options: […]

快速实现HMAC和SHA1encryption

我相对较新的Swift,我卡住encryption使用HMAC和SHA1。 我发现以下回答https://stackoverflow.com/a/24411522/4188344,但我不能解决如何正确实施。 任何帮助将是惊人的。

如何快速传递多个值与通知

如何通过通知发送一个数字和一个string… let mynumber=1; let mytext="mytext"; NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????); 并在接收器中接收值? func refreshList(notification: NSNotification){ let receivednumber=?????????? let receivedString=????????? }

Swift – 如何将两个视图控制器链接到一个容器视图,并使用分段控制在它们之间切换?

现在我有一个视图控制器包含1个分段控制和2个UI视图。 但是我认为更新用户界面视图来增强未来的编辑过于复杂。 我正在使用隐藏的方法。 import UIKit class PopularHistoryViewController: UIViewController { @IBOutlet weak var segmentedControl: UISegmentedControl! @IBOutlet weak var popularView: UIView! @IBOutlet weak var historyView: UIView! @IBAction func indexChanged(sender: UISegmentedControl) { switch segmentedControl.selectedSegmentIndex { case 0: NSLog("Popular selected") //show popular view popularView.hidden = false historyView.hidden = true case 1: NSLog("History selected") //show history view popularView.hidden = true […]

UIDevice currentDevice模型可能的值

什么是[[UIDevice currentDevice] model];返回的所有可能的值[[UIDevice currentDevice] model]; ? 没有logging 。