swift2

Using termios in Swift

元气小坏坏 提交于 2020-08-23 06:13:38
问题 Now that we've reached Swift 2.0, I've decided to convert my, as yet unfinished, OS X app to Swift. Making progress but I've run into some issues with using termios and could use some clarification and advice. The termios struct is treated as a struct in Swift, no surprise there, but what is surprising is that the array of control characters in the struct is now a tuple. I was expecting it to just be an array. As you might imagine it took me a while to figure this out. Working in a Playground

Using termios in Swift

蓝咒 提交于 2020-08-23 06:13:25
问题 Now that we've reached Swift 2.0, I've decided to convert my, as yet unfinished, OS X app to Swift. Making progress but I've run into some issues with using termios and could use some clarification and advice. The termios struct is treated as a struct in Swift, no surprise there, but what is surprising is that the array of control characters in the struct is now a tuple. I was expecting it to just be an array. As you might imagine it took me a while to figure this out. Working in a Playground

Convert Objective-C enum to Swift String

两盒软妹~` 提交于 2020-07-23 03:13:08
问题 I'm trying to convert iOS error codes to String in Swift 2 (XCode 7.2). But converting to String returns the type name instead of the value name for system enums. This is what I'm trying: import CoreLocation import EventKit let clError = CLError.LocationUnknown let clErrorString = "\(clError)" // EXPECTED: 'LocationUnknown'. OBTAINED: 'CLError' let ekError = EKErrorCode.CalendarIsImmutable let ekErrorString = "\(ekError)" // EXPECTED: 'CalendarIsImmutable'. OBTAINED: 'EKErrorCode' But with

Convert Objective-C enum to Swift String

大兔子大兔子 提交于 2020-07-23 03:11:11
问题 I'm trying to convert iOS error codes to String in Swift 2 (XCode 7.2). But converting to String returns the type name instead of the value name for system enums. This is what I'm trying: import CoreLocation import EventKit let clError = CLError.LocationUnknown let clErrorString = "\(clError)" // EXPECTED: 'LocationUnknown'. OBTAINED: 'CLError' let ekError = EKErrorCode.CalendarIsImmutable let ekErrorString = "\(ekError)" // EXPECTED: 'CalendarIsImmutable'. OBTAINED: 'EKErrorCode' But with

Swift 2 Count elements of array matching condition

蹲街弑〆低调 提交于 2020-07-18 12:49:08
问题 I'm basically looking for the swift equivalent of the follow c++ code: std::count_if(list.begin(), list.end(), [](int a){ return a % 2 == 0; }); // counts instances of even numbers in list My problem isn't actually searching for even numbers, of course; simply the general case of counting instances matching a criterion. I haven't seen a builtin, but would love to hear that I simply missed it. 回答1: Like this: let a: [Int] = ... let count = a.filter({ $0 % 2 == 0 }).count 回答2: An alternative to

Swift 2 Count elements of array matching condition

给你一囗甜甜゛ 提交于 2020-07-18 12:49:06
问题 I'm basically looking for the swift equivalent of the follow c++ code: std::count_if(list.begin(), list.end(), [](int a){ return a % 2 == 0; }); // counts instances of even numbers in list My problem isn't actually searching for even numbers, of course; simply the general case of counting instances matching a criterion. I haven't seen a builtin, but would love to hear that I simply missed it. 回答1: Like this: let a: [Int] = ... let count = a.filter({ $0 % 2 == 0 }).count 回答2: An alternative to

How to share both Image and Text together in swift?

感情迁移 提交于 2020-07-17 09:33:26
问题 I am trying to share both image and text in swift. but when i choose to share via facebook, messenger or whatsapp it only gives text (image is not shared). I am using UIActivityViewController for sharing. here is my code: func displayShareSheet(latitude:NSString?, longitude:NSString?, image:UIImage?, address:NSString? ) { let activityViewController = UIActivityViewController(activityItems: [(latitude as NSString?)!, (longitude as NSString?)!, (image as UIImage?)!, (address as NSString?)!],

How to pass a Swift type as a method argument?

守給你的承諾、 提交于 2020-07-14 16:27:37
问题 I'd like to do something like this: func doSomething(a: AnyObject, myType: ????) { if let a = a as? myType { //… } } In Objective-C the class of class was Class 回答1: You have to use a generic function where the parameter is only used for type information so you cast it to T : func doSomething<T>(_ a: Any, myType: T.Type) { if let a = a as? T { //… } } // usage doSomething("Hello World", myType: String.self) Using an initializer of the type T You don’t know the signature of T in general

Schedule Local Notifications at Specific times in the day

偶尔善良 提交于 2020-06-11 09:44:20
问题 I want to setup a daily notification system for my app. It should notify the user twice a day, once at 8:00 AM (Hey there, your set of morning doses is ready. Wanna check it out?) and once at 7:00 PM (Ssup! Your evening Dose is waiting inside). I know that by doing this i'll run out of notifications in a month since there's a 64 notifications cap (for local notifications) but by then, the app will be live and i'll be done setting up remote notifications update. I've looked at this question:

RxSwift table view with multiple custom cell types

*爱你&永不变心* 提交于 2020-06-10 07:28:26
问题 I'w wonder is there any code example for RxSwift when I can use multiple custom cells inside one table view. So for example I have two section and first section has 10 cells with type CellWithImage identifier and second section has 10 cells with type CellWithVideo identifier. All tuts and code examples which I've founded are using only one cell type, for instance RxSwiftTableViewExample Thanks for any help 回答1: I've managed it using RxSwiftDataSources, it allow you to use custom cells with