swift2

How to compose multi-character emoji from raw hex

▼魔方 西西 提交于 2020-01-15 05:33:06
问题 I'm getting JSON like this from the server: { "unicode":"1f468-1f468-1f467-1f467" } and I'm supposed to translate it into its composite character for display and/or copying to the pasteboard: 👨‍👨‍👧‍👧 The solution so far comes from this SO question: let u = json["unicode"] as? String let dashless = u.characters.split{$0 == "-"}.map(String.init) let charArray = dashless.map { char -> Character in let code = Int(strtoul(char, nil, 16)) return Character(UnicodeScalar(code)) } let unicode = String

In Swift, how do I animate a UIView with auto-layout like a page sliding in?

纵饮孤独 提交于 2020-01-15 03:57:05
问题 I try to create a UIView that is representing a page whose size is the same as the device screen. Since the app supports orientation, I am using AutoLayout to construct it. It works fine until I try to animate the page to slide in from the right. Here is the best I could come up with after some research: myView = UIView() myView.backgroundColor = UIColor.orangeColor() parentView.addSubview(myView) myView.translatesAutoresizingMaskIntoConstraints = false myView.leftAnchor

iOS9 FileManager File permissions change

社会主义新天地 提交于 2020-01-15 01:48:17
问题 After switching my application to iOS9 I started to get errors that the files I was writing were not readable. Here is how I create the files let fileManager = NSFileManager.defaultManager() let directory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] let path = "\(directory)/file.txt" let attributes: [String:AnyObject] = [NSFilePosixPermissions: NSNumber(short: 666)] let success = fileManager.createFileAtPath(path, contents: nil, attributes: attributes)

Receive Custom Notification iOS swift 2.0

雨燕双飞 提交于 2020-01-14 13:57:55
问题 I tried to receive my custom notification from my web through parse website. Code didReceiveRemoteNotification : func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { PFPush.handlePush(userInfo) if application.applicationState == UIApplicationState.Inactive { PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)} Here is the JSON i received from parse website : [aps: { alert = "test adirax"; sound = default; }] It works well

Swift 2 base64 Encoding Yield Different Results

China☆狼群 提交于 2020-01-14 12:12:41
问题 Swift 1.2 let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD); let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!; var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(nil); Swift 2 let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD); let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!; var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions

Swift 2 parse Json as Optional to array

十年热恋 提交于 2020-01-14 10:45:09
问题 I am getting list of countries from a web service. After receiving it I used this code to process it: 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

How to use swift files in objective c

可紊 提交于 2020-01-14 09:07:08
问题 I want to use my Swift files in Objective-C code. I found different link that say how to do it. My project name is: Test-Project. I imported #import "Test-Project-Swift.h" to my .m file to use needed classed in objective c source code. I watched this video and there is no problem, but I have 'Test-Project-Swift.h' file not found. 回答1: As we recognized: It looks like the problem with "-" we need to change it to "_" - "Test_Project-Swift.h". I've checked Objective-C Generated Interface Header

Converting a Swift UnsafePointer<AudioStreamBasicDescription> to a Dictionary?

半世苍凉 提交于 2020-01-14 05:34:11
问题 I want to know how to create a dictionary of [String:AnyObject] from an UnsafePointer<AudioStreamBasicDescription> I guess I don't understand how to work with an UnsafePointer<T> in Swift . Here's where I'm starting from - The AVAudioFile class has a fileFormat property which is of AVAudioFormat Type. AVAudioFormat has a streamDescription property which returns an UnsafePointer<AudioStreamBasicDescription> as a read-only property. I'd like to see what the values are in this struct and

guard let error: Initializer for conditional binding must have Optional type not 'String'

人盡茶涼 提交于 2020-01-13 19:56:09
问题 I got fatal error While using guard let. Here's the error: Initializer for conditional binding must have Optional type not 'String' Below my code which i have used: @IBAction func signUpButtonPressed(sender: UIButton) { guard let email = emailTextField.text! where emailTextField.text!.characters.count > 0 else { // alert return } guard let password = passwordTextField.text! where passwordTextField.text!.characters.count > 0 else { // alert return } self.registerUserAsync(email, password:

Iterating over an array by range

☆樱花仙子☆ 提交于 2020-01-13 14:01:10
问题 I have an array: [1, 2, 3, 4, 5, 6...100] I am looking to iterate over this array by 5, specifically: Take the first 5 numbers of the array and get the average, move on to the next 5 numbers and get the average, and so on.. I have tried numerous methods such as a Dequeue and for loops but have not been able to get the outcome desired. 回答1: You need to use a progression loop to iterate each 5 element and use reduce to sum the subsequence then divide the total by the subsequence elements count: