swift2

Find closest item in array that matches criteria in Swift?

假如想象 提交于 2021-01-28 21:43:58
问题 When I have a reference to an item in an array, I'd like to find another item closest to it that matches a certain criteria (forward or backwards). For example, I have this array: let items = [ (a: "Item 1", b: "F", c: 3), (a: "Item 2", b: "S", c: 5), (a: "Item 3", b: "D", c: 7), (a: "Item 4", b: "A", c: 9), (a: "Item 5", b: "M", c: 11), (a: "Item 6", b: "I", c: 13), (a: "Item 7", b: "F", c: 15), (a: "Item 8", b: "S", c: 17), (a: "Item 9", b: "D", c: 19), (a: "Item 10", b: "A", c: 21), (a:

How to check 3G,4G and wifi internet connection in swift 2.2

若如初见. 提交于 2021-01-28 09:50:51
问题 import Foundation import SystemConfiguration public class Reachability { class func isConnectedToNetwork() -> Bool { var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) let defaultRouteReachability = withUnsafePointer(&zeroAddress) { SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))

CAShapeLayer. Does the line pass through the point?

戏子无情 提交于 2021-01-28 05:12:29
问题 I use CAShapeLayer in order to draw a line on the screen. In the method touchesEnded I want to check " Does the line pass through the point?". In my code when I press on the any part of the screen the method contains returns always true. Perhaps, I have problem in line.frame = (view?.bounds)! . How can I fix it? Sorry for my bad English. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first let firstPosition = touch?.location(in: self) if

Value of type 'AppDelegate' has no member 'window'

元气小坏坏 提交于 2021-01-26 19:09:56
问题 Im trying to use the 3D touch Quick Actions and I'm setting it up copying VEA Software code. In his sample code it works perfectly but when I try to add it to my app I get some unusual errors. I am new to coding and swift so please explain as much as possible. Thanks. Below I have the code which is in my app delegate. This is where I'm getting the error ( self.window? ): @available(iOS 9.0, *) func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool { var handled = false var

Value of type 'AppDelegate' has no member 'window'

て烟熏妆下的殇ゞ 提交于 2021-01-26 19:09:19
问题 Im trying to use the 3D touch Quick Actions and I'm setting it up copying VEA Software code. In his sample code it works perfectly but when I try to add it to my app I get some unusual errors. I am new to coding and swift so please explain as much as possible. Thanks. Below I have the code which is in my app delegate. This is where I'm getting the error ( self.window? ): @available(iOS 9.0, *) func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool { var handled = false var

Cannot find char at position and index of char of a string var in Swift 2

夙愿已清 提交于 2021-01-07 03:12:43
问题 Equivalent functions in Swift 2 of Java's charAt() and indexOf() ? 回答1: Did you read the NSString documentation? String and NSString are not identical, but as mentioned by this answer most functions are more or less the same. Anyway, the two exact functions you ask for are right there: Java's charAt : func characterAtIndex(_ index: Int) -> unichar Java's indexOf : func rangeOfString(_ searchString: String) -> NSRange The NSRange data type contains just 2 variables: location and length that

MAC OS Xcode Swift 2.2 Fullscreen Mode

我们两清 提交于 2020-12-05 11:27:07
问题 What Swift code will switch the app to fullscreen? I found references with example code for IOS. I am looking for a code which works for a MacOS app. 回答1: One way is to override viewDidAppear in NSViewController : class ViewController : NSViewController { override func viewDidAppear() { let presOptions: NSApplicationPresentationOptions = ([.FullScreen,.AutoHideMenuBar]) let optionsDictionary = [NSFullScreenModeApplicationPresentationOptions : NSNumber(unsignedLong: presOptions.rawValue)] self