Tag: swift2

我如何添加滚动视图到我的gameScene.swift,这将滚动一系列的精灵?

所以我创build了一个3×10的精灵网格,它离开了屏幕的底部边缘。 我已经添加了一个UIScrollView,滚动指示器出现,但实际上没有发生移动。 代码如下: import SpriteKit var buyButton = SKSpriteNode() var pic = SKTexture(imageNamed: "Button") class GameScene: SKScene, UIScrollViewDelegate { var scrollView = UIScrollView() var scrollFrame = CGRect!() override func didMoveToView(view: SKView) { /* Setup your scene here */ print(self.frame.size.width) scrollView = UIScrollView(frame: CGRect(origin: CGPointMake(0,0), size: CGSizeMake(self.frame.size.width, self.frame.size.height))) scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*2) scrollView.bounces = false scrollView.pagingEnabled […]

检查一个数组是否包含Swift中另一个元素的所有元素

我想写一个数组的扩展来检查一个数组是否包含另一个数组的所有元素,在我的使用情况下它是string对象,但我一直得到: Cannot convert value of type 'T.Generator.Element' to expected argument type '@noescape _ throws -> Bool' 在行self.contains(item)关于item的错误 这里是我的代码: extension Array { func containsArray<T : SequenceType where T.Generator.Element : Equatable> (array:T) -> Bool{ for item:T.Generator.Element in array{ if !self.contains(item) { return false } } return true } }

使用iPhone作为iBeacon发射机

我正在试图设置我的iPhone来广播另一个iPhone的iBeacon信号。 应该拿起信号的iPhone正在工作,但我不能让另一个iPhone来传播信号。 我正在学习本教程: https : //www.hackingwithswift.com/example-code/location/how-to-make-an-iphone-transmit-an-ibeacon 这是我的代码: import CoreBluetooth import CoreLocation class iBeaconTransmission: NSObject, CBPeripheralManagerDelegate { var localBeacon: CLBeaconRegion! var beaconPeripheralData: NSDictionary! var peripheralManager: CBPeripheralManager! func initLocalBeacon() { if localBeacon != nil { stopLocalBeacon() } let localBeaconUUID = "placeholder" let localBeaconMajor: CLBeaconMajorValue = 123 let localBeaconMinor: CLBeaconMinorValue = 456 let uuid = NSUUID(UUIDString: localBeaconUUID)! localBeacon […]

在Swift 2.0中更改AVAudioPlayer的rate var时发生声音失真

我正在制作一个简单的audio播放应用程序。 它有大约10个audio文件,每个文件的正常播放速度为100次/分钟。 用户可以在调用play()函数之前input一个速度variables(70到140 bpm之间),该速度variables被赋予(速度/ 100)到AVAudioPlayer速率var。 @IBAction func playPause(sender: AnyObject) { if !isPlaying { let audioPath = NSBundle.mainBundle().pathForResource(selectedTrack, ofType: "mp3") do { try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!)) player.enableRate = true player.rate = Float(tempo) / 100 player.play() isPlaying = !isPlaying } catch { print ("play error") } } } 以audio正常速度播放(100b.pm)的效果非常好。 但是,通过改变速度甚至每单一bpm单位,播放听起来真的很差。 速度转换听起来准确(即降低速度变化导致audio变慢,反之亦然),并且音调听起来像保持(尽pipe在持续音符中有点“摇晃”),但音质似乎受到主要方面的负面影响。 我也许会期望这个更为剧烈的利率变动(利率<50%或利率> 200%),但即使在99%和101%的情况下也是非常明显的。 我正在使用44k / […]

从HealthKit获取昨天的步骤

我正在构build一个个人使用的应用程序,而且我目前正在坚持如何准确地从healthkit得到昨天的步骤。 然后从那里,把它放到一个variables(应该很容易,我知道)。 我有一个HealthKitManager类,从视图内调用该函数,然后将其附加到来自同一视图的variables。 我已经search了大部分健康问题的资料,并且找回数据,但是我不认为这是准确的数据。 我昨天的电话数据是1442步,但是它返回了2665步。 最重要的是,当我试图把数据是一个variables,打印出0。 HealthKitManagerClass import Foundation import HealthKit class HealthKitManager { let storage = HKHealthStore() init() { checkAuthorization() } func checkAuthorization() -> Bool { // Default to assuming that we're authorized var isEnabled = true // Do we have access to HealthKit on this device? if HKHealthStore.isHealthDataAvailable() { // We have to […]

Swift 2将Jsonparsing为数组的可选项

我从Web服务获取国家列表。 收到它后,我用这个代码来处理它: if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary { // triggering callback function that should be processed in the call // doing logic } else { if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? AnyObject { completion(json) } else { let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) print("Error could not parse […]

不能下标types的值? 使用Stringtypes的索引

我知道有很多相同的问题,但仍然找不到解决我的错误的方法。 请参阅图像的更多细节。 我使用Xcode 7和Swift 2.0 编辑:打击斯威夫特的警告。 finnaly (change?[NSKeyValueChangeNewKey]?.boolValue)! 修复了错误

Swift 2中没有后退button

我更新了我的项目Swift 2.0。 当我从UITableViewController移动到UIViewController ,导航项不显示返回button。 每个控制器都有UINavigationController,我使用show segue。 这个代码当我移动到其他控制器 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None) if searchPredicate == nil { performSegueWithIdentifier("listenMusic", sender: self) } else { performSegueWithIdentifier("oneSound", sender: self) } } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "listenMusic" { if (UIApplication.sharedApplication().delegate as! AppDelegate).mainAudioPlayer != nil { […]

我们如何在一个应用程序中使用HTTP和HTTPS两种ATS(应用程序传输安全性)?

苹果公司针对iOS 9和OSX 10.11 El Capitan宣布了“App Transport Security”。 iOS 9的“iOS 新增function ”指南解释说: 应用程序传输安全性(ATS)允许应用程序向其Info.plist文件添加声明,以指定需要安全通信的域。 ATS防止意外泄露,提供安全的默认行为,并且易于采用。 您应该尽快采用ATS,无论您是创build新应用还是更新现有应用。 如果我们想删除或禁用ATS意味着我们只想使用HTTP,那么我们在.plist文件中进行input,如下所示: <key>NSAppTransportSecurity</key> <dict> <!–Include to allow all connections (DANGER)–> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 如果我们的域名在HTTPS中,我们正在进入.plist文件,如下所示: <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yourserver.com</key> <dict> <!–Include to allow subdomains–> <key>NSIncludesSubdomains</key> <true/> <!–Include to allow HTTP requests–> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <!–Include to specify minimum TLS version–> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>TLSv1.1</string> </dict> […]

接收自定义通知iOS Swift 2.0

我试图通过parsing网站从我的网站收到我的自定义通知。 代码didReceiveRemoteNotification: func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { PFPush.handlePush(userInfo) if application.applicationState == UIApplicationState.Inactive { PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)} 这是我从parsing网站收到的JSON: [aps: { alert = "test adirax"; sound = default; }] 它适合我,并且通知显示出来。 但是,当我试图从我的网站推送数据,通知不能显示/popup。 这是我的JSON看起来像: {"aps": {"alerts" : "test", "links": "", "sounds": "default"}} 我试图打印(userInfo),结果是我得到像上面的所有数据,但没有notif。 我想我错过了一些代码来转换数据? 信息 对于具体的信息,我试图通过订阅“渠道”接收来自parse.com的通知。 所以这不是一个本地通知或其他。 添加代码(根据我的JSONtypes) if let launchOptions = launchOptions { let userInfo = […]