Tag: 迅速的

Swift字典获取价值的关键

我正在使用types[UIImage:UIImage]的快速字典,我试图find一个给定值的特定键。 在Objective-C中,我可以使用allKeysForValue,但似乎没有这样的Swift字典的方法。 我应该使用什么?

检查是否在Core Data中设置了属性?

我如何检查一个属性是否在核心数据对象中设置? 我将所有的核心数据对象加载到一个目录中: var formQuestions = [Questions]() 而我的核心数据NSManagementObject是: @NSManaged var noticeText: String formQuestions [indexPath.row] .noticeText //加载: var fetchRequest = NSFetchRequest(entityName: "Questions") fetchRequest.predicate = NSPredicate(format: "forms = %@", currentForm!) formQuestions = context.executeFetchRequest(fetchRequest, error: nil) as [Questions] 我的属性“noticeText”可能是空的或不是,所以当我创build我的核心数据对象时,可能没有设置一些值。 (该属性在核心数据中设置为可选) 当我现在尝试certificate如果有价值,它总是给我一个“EXC_BAD_ACCESS ….” if(formQuestions[indexPath.row].noticeText.isEmpty == false) 我可以设置一个空string,当我创build我的核心数据对象,但这应该不是一个好的解决scheme。 那么如何检查(optinal)而不是设定值? 提前致谢。

如何将敌人移动到移动的玩家?

我正在创build一个简单的精灵套装游戏,将玩家置于屏幕的左侧,而敌人从右侧接近。 由于玩家可以上下移动,我希望敌人“巧妙地”调整他们的路线。 我试着在玩家移动的时候删除和重新添加SKAction序列,但是下面的代码会导致敌人根本不显示,可能是因为它只是在每次帧更新时添加和删除每个动作,所以他们从来没有机会移动。 希望得到一些关于创造“聪明”敌人的最佳做法的小反馈,这些敌人将随时转向玩家的位置。 这是我的代码: func moveEnemy(enemy: Enemy) { let moveEnemyAction = SKAction.moveTo(CGPoint(x:self.player.position.x, y:self.player.position.y), duration: 1.0) moveEnemyAction.speed = 0.2 let removeEnemyAction = SKAction.removeFromParent() enemy.runAction(SKAction.sequence([moveEnemyAction,removeEnemyAction]), withKey: "moveEnemyAction") } func updateEnemyPath() { for enemy in self.enemies { if let action = enemy.actionForKey("moveEnemyAction") { enemy.removeAllActions() self.moveEnemy(enemy) } } } override func update(currentTime: NSTimeInterval) { self. updateEnemyPath() }

如何播放背景Swift中的audio?

正如你所看到的,我正在播放一个audio广播。 但是,当我按主页button,并退出应用程序stream停止或我听不到。 如何在后台继续stream式传输并从locking屏幕上收听? ViewController.Swift import UIKit import AVFoundation import MediaPlayer import GoogleMobileAds class ViewController: UIViewController, GADInterstitialDelegate { @IBOutlet weak var exitMapButton: UIButton! @IBOutlet weak var radarMap: UIWebView! var interstitial: GADInterstitial! func createAndLoadInterstitial() -> GADInterstitial { var interstitial = GADInterstitial(adUnitID: "ca-app-pub-5378899862041789/2782958552") interstitial.delegate = self interstitial.loadRequest(GADRequest()) return interstitial } func getAd(){ if (self.interstitial.isReady) { self.interstitial.presentFromRootViewController(self) self.interstitial = […]

在Swift中实现failable初始化器的最佳实践

使用下面的代码,我尝试定义一个简单的模型类,它是一个failable初始值设定项,它以(json-)字典作为参数。 如果在原始json中没有定义用户名,则初始化程序应返回nil 。 1.为什么代码不能编译? 错误消息说: 所有存储的类实例的属性必须在初始化程序返回nil之前进行初始化。 这没有意义。 为什么我应该在计划返回nil初始化这些属性? 2.我的方法是正确的,还是会有其他的想法或共同的模式来实现我的目标? class User: NSObject { let userName: String let isSuperUser: Bool = false let someDetails: [String]? init?(dictionary: NSDictionary) { if let value: String = dictionary["user_name"] as? String { userName = value } else { return nil } if let value: Bool = dictionary["super_user"] as? Bool { isSuperUser […]

为什么在一个协议中只能得到一个只能得到的属性需求才能被一个符合的属性满足?

为什么下面的代码会产生一个错误? protocol ProtocolA { var someProperty: ProtocolB { get } } protocol ProtocolB {} class ConformsToB: ProtocolB {} class SomeClass: ProtocolA { // Type 'SomeClass' does not conform to protocol 'ProtocolA' var someProperty: ConformsToB init(someProperty: ConformsToB) { self.someProperty = someProperty } } 这个类似的问题的答案是有道理的。 但是,在我的示例中,该属性是只能获取的。 为什么不能这样工作? 这是Swift的一个缺点,还是有一些原因,这是有道理的?

如何在Swift中打印variables的types或类?

有没有办法在swift中打印variables的运行时types? 例如: var now = NSDate() var soon = now.dateByAddingTimeInterval(5.0) println("\(now.dynamicType)") // Prints "(Metatype)" println("\(now.dynamicType.description()") // Prints "__NSDate" since objective-c Class objects have a "description" selector println("\(soon.dynamicType.description()") // Compile-time error since ImplicitlyUnwrappedOptional<NSDate> has no "description" method 在上面的例子中,我正在寻找一种方法来显示variables“很快”的types是ImplicitlyUnwrappedOptional<NSDate> ,或者至lessNSDate! 。

Swift语言中的error handling

我没有太多的读到Swift,但是我注意到的一点是没有例外。 那么他们如何在Swift中进行error handling呢? 有没有人发现任何与error handling有关的东西?

在Swift中使用dispatch_once单例模型

我试图找出一个合适的单例模型在Swift中的使用。 到目前为止,我已经能够得到一个非线程安全的模型,其工作原理如下: class var sharedInstance:TPScopeManager { get { struct Static { static var instance : TPScopeManager? = nil } if !Static.instance { Static.instance = TPScopeManager() } return Static.instance! } } 在Static结构中包装单例实例应该允许单个实例不会与单例实例发生冲突,而没有复杂的命名规则,它应该使事情相当私密。 显然,这个模型并不是线程安全的,所以我试图添加dispatch_once到整个事情: class var sharedInstance:TPScopeManager { get { struct Static { static var instance : TPScopeManager? = nil static var token : dispatch_once_t = 0 […]