Tag: swift2

如何观察价值的变化并显示出来? 在Swift中

我有一个variables,在一个不同的类中的数组中的对象的数量,我怎么能跟踪当前类中的这个variables的变化? 我做了很多不同的尝试,但都失败了。 var digitIndex: Int! { set { self.digitIndex = newValue } get { return firstClass.indexesOfSelectedNodes().count } } override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if context == &digitIndex { if let newValue = change?[NSKeyValueChangeNewKey] { print("newValue") infoLabel.text = "\(newValue)" } } }

在Xcode 7中播放audio/ Swift 2不工作

我目前在Xcode 7 beta 4上使用Swift 2,试图播放audio。 我的代码中没有出现任何错误,但是当按下播放button时,audio不能播放。 我弄不清楚什么是错的。 import UIKit import AVFoundation class VesuviusViewController: UIViewController { @IBOutlet weak var PausePlay: UIButton! func playVes() { do { _ = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!)) } catch { print("Error") } } @IBAction func playButtonPressed(sender: AnyObject) { playVes() } override func viewDidLoad() { super.viewDidLoad() // Do any additional […]

将Swift UnsafePointer <AudioStreamBasicDescription>转换为字典?

我想知道如何从UnsafePointer<AudioStreamBasicDescription>创build[String:AnyObject]的字典, 我想我不明白如何在Swift使用UnsafePointer<T> 。 这里是我从哪里开始 – AVAudioFile类有一个AVAudioFormattypes的fileFormat属性。 AVAudioFormat有一个streamDescription属性,它返回一个UnsafePointer<AudioStreamBasicDescription>作为只读属性。 我想看看这个结构中的值是什么,转换成一个字典似乎可能是合理的目标。 实际上,在AVAudioFormat类中似乎已经有一个“设置”属性,但是我有兴趣理解访问存储在UnsafePointer的值的“正确方法”。 从文档Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html 检查结构是否不为零后有没有办法做一个不安全的转换? 我会在这里使用unsafeBitCast吗? 我太犹豫了,因为我读过这个“非常危险的” 我意识到我可以通过以下方式访问底层内存: let audioFileURL:NSURL = NSBundle.mainBundle().URLForResource("FILENAME", with Extension: "mp3") var file:AVAudioFile? do { file = try AVAudioFile(forReading: audioFileURL) guard let file = file else { fatalError("file must not be nil") } […]

添加一个项目到一个核心数据一对多关系的NSSet

我有一个核心数据关系,一个实体拥有另一个实体。 据我所知,许多类的每个实例都在NSSet中? 在一个class里面 (?) 我的问题是 – 什么是最好的方式添加到这个集合的项目? 我认为这一定是一个非常普遍的问题 – 但我似乎无法find一个简单的方法。 这是我的尝试:(这一切都来自一个class级) static var timeSlotItems: NSSet? //The Set that holds the many? … static func saveTimeSlot(timeSlot: TimeSlot) { //TimeSlot is the many object retrieveValues() var timeSlotArray = Array(self.timeSlotItems!) timeSlotArray.append(timeSlot) var setTimeSlotItems = Set(timeSlotArray) self.timeSlotItems = setTimeSlotItems // This is the error line } 其中retrieveValues()只是更新类中的所有coreData值。 TimeSlot是我想要添加的许多对象。 我在最后一行得到一个错误,错误是:“不能调用types为Array的参数为List的types为Set […]

我传入NSUserDefaults.synchronize()的参数是什么?

我试图在我的精灵游戏中保存高分。 根据我看过的所有教程和我读过的堆栈溢出答案,下面的代码应该可以工作: NSUserDefaults.standardUserDefaults().setInteger(highScore, forKey: "highScore") NSUserDefaults.synchronize() 但是我不断遇到一个错误 NSUserDefaults.synchronize() 部分错误状态“参数#1在调用中缺less参数”我所看过的所有地方似乎使用该代码没有错误。 我知道最近会有一个快速更新。 这个更新是否会导致同步function发生变化,我该如何解决这个问题?

本地通知每一天的特定日子

我需要创build一个特定时间的本地通知(例如在18.00)解雇了每星期的工作日(不是星期六和星期天),但我找不到示例或教程。 我想用swift2.1。 我怎样才能创build这个通知? 我的问题是正确定义通知

在Swift中查找string中的子串的第N个实例的索引

我的Swift应用程序涉及在UITextView中search文本。 用户可以在该文本视图中search特定的子string,然后在文本视图中跳转到该string的任何实例(比如第三个实例)。 我需要找出他们在哪个字符的整数值。 例如: 示例1:用户search“hello”,文本视图显示“hello you hello,hello you hello”,然后用户按下箭头查看second实例。 我需要知道第二个hello中的第一个h的整数值(即,hello中的h个字符在文本视图中)。 整数值应该是22 。 例2:用户search“abc”,而文本视图为“abcd”,他们正在寻找abc的第一个实例,所以整数值应该是1 (这是a的整数值,因为它是第一个字符他们正在search的实例)。 我怎样才能得到用户正在寻找的字符的索引?

操作button不会在本地通知中显示ios 9.2 swift 2

我正在循环中创build通知,相关的代码是: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { appDelegate = self serNotificationType = UIUserNotificationType.Alert.union(UIUserNotificationType.Sound).union(UIUserNotificationType.Badge) let completeAction = UIMutableUserNotificationAction() completeAction.identifier = "COMPLETE" // the unique identifier for this action completeAction.title = "Clear" // title for the action button completeAction.activationMode = .Background // UIUserNotificationActivationMode.Background – don't bring app to foreground completeAction.authenticationRequired = false // […]

UITabBarController和UINavigationController中的特定UIViewController中的设备旋转

基本上,这是我的页面结构,其中: TBC = UITabBarController; VC = UIViewController; NVC = UINavigationViewController +–> [NVC] –> [VC] –> … –> [VC] Home | [VC] –> [TBC] –+–> [NVC] –> [VC] –> … –> [VC] | +–> [NVC] –> [VC] –> … –> [VC] 我希望一些具体的VC可以被允许在不同的方向上查看。 最可能的是,这些将是叶节点VC 。 这可能是显示图片或其他内容的页面。 大部分的应用程序只能以纵向浏览。 想想Facebook应用程序,其中大多数核心页面都是以纵向浏览的,而图片/video可以纵向或横向浏览。

如何在Swift 2中切换到前置摄像头?

我有我的ViewDidAppear所有这些代码 captureSession = AVCaptureSession() // let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) var captureDevice:AVCaptureDevice! = nil if (camera == CameraType.Front) { let videoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) for device in videoDevices{ let device = device as! AVCaptureDevice if device.position == AVCaptureDevicePosition.Front { captureDevice = device break } } } else { captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) } do { let input = […]