swift3

Reading from txt file in Swift 3

半世苍凉 提交于 2020-01-03 13:17:13
问题 I want to know how to read from a txt file and print out specific parts of the file? For example, "test.txt" will contain: '''Jason 16 male self programing Josh 15 male friend art''' So I am looking for a way to print each word and line separately. Such as only printing: "Jason" "Jason is 16" "Josh likes art" This is what I got so far from searching around if let filepath = Bundle.main.path(forResource: "test", ofType: "txt") { do { let contents = try String(contentsOfFile: filepath) print

Is there any difference at all between suffix(from:) and dropFirst(_:)?

心不动则不痛 提交于 2020-01-03 08:57:10
问题 It just occurred to me that when working with subsequences in Swift, func suffix(from: Int) seems to be identical to just dropFirst(_:) (Obviously, you just change the input value from say "3" to "7" in the case of an array of length "10".) Just to repeat that. So: of course, for an array of say length ten. What I mean is func suffix(from: Int) with "2" would be the same as dropFirst(_:) with "8" , for example. Similarly upTo / through seem to be identical to dropLast(_:) Other than

Convert single file to Swift 3 in Xcode 8?

不羁岁月 提交于 2020-01-03 08:52:51
问题 Is there any way in Xcode 8 or 8.1 to do a Swift 2.X -> Swift 3 migration for a single file in a project that's already been converted to Swift 3? For technical debt/legacy build process reasons, we are currently having to maintain both Xcode 7 and Xcode 8 versions of our project. This is causing headaches when we have to merge new development from Xcode 7 over to Xcode 8 . Since the project has already been fully converted to Swift 3 in our Xcode 8 branch (including a ton of hand fixes), we

send data from tableviewcontroller to detailviewcontroller

耗尽温柔 提交于 2020-01-03 04:40:09
问题 I have a tableviewcontroller named DetailTableViewController and I need to pass data to next tableviewcontroller named DetailTableViewController. But I need to pass the data as the user clicks the cell of DetailTableViewController. Then, I got error as "unexpectedly found nil while unwrapping an Optional value".Please help me sort out in Swift 3. 回答1: First create a variable in your detail view controller to hold the value which you send from first view controller. then assign the value

Swift manually rotate view controller

北城以北 提交于 2020-01-03 03:45:11
问题 So I'm working on an iOS 10 app using Swift 3 and Xcode 8.3.3. In my app I have to take successively 8 pictures with indicators on the camera. So I'm using AVCaptureVideoPreviewLayer for the custom camera and I need to set the view displaying the camera on landscape mode. I did it using this extension that I found on stackoverflow while searching for the problem. struct AppUtility { static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { if let delegate = UIApplication.shared

Access static property of protocol extension

巧了我就是萌 提交于 2020-01-03 03:06:07
问题 I'm trying to build a protocol exposing a static property, then use that static property in an extension of that protocol, but it seems to work only if I define this static property in the protocol extension as well. Basically the code I'm trying to get to work: protocol NibInstantiable: class { static var bundle: Bundle? { get } static var nibName: String { get } } extension NibInstantiable where Self: UIViewController { // static var nibName: String { // return "" // } static func

How to send NSData over an OutputStream

筅森魡賤 提交于 2020-01-03 03:01:16
问题 You can view this project on github here: https://github.com/Lkember/MotoIntercom/ The class that is of importance is PhoneViewController.swift I have an AVAudioPCMBuffer. The buffer is then converted to NSData using this function: func audioBufferToNSData(PCMBuffer: AVAudioPCMBuffer) -> NSData { let channelCount = 1 let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: channelCount) let data = NSData(bytes: channels[0], length:Int(PCMBuffer.frameCapacity * PCMBuffer

Deploying Vapor with heroku

允我心安 提交于 2020-01-03 02:49:29
问题 I have created a very simple app with one route in main.swift . It reads a city from a query string and use it to get the weather (through the yahoo API) of that city. this is the route: drop.get("whether") { request in guard let city = request.data["city"]?.string else { return try JSON(node: ["Error": "no city given"]) } return try drop.client.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)

How to give width and height programatically for collection view cell in ios, swift 3

为君一笑 提交于 2020-01-03 02:01:06
问题 I tried to give the height and width for collectionViewCell , programatically. I used following method: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let viewWidth = self.deviceWidth/3 - 5 let viewHeight = viewWidth return CGSize(width :100,height: 100) } This didn't work. even this method didn't show as a hint. Help me with this. Is there any other delegate method to set the cell

iOS 11 ARKit : Drag Object in 3D View

六眼飞鱼酱① 提交于 2020-01-03 01:46:23
问题 I have a node object in 3d view and i need to drag that object, So far i have tried from here : Placing, Dragging and Removing SCNNodes in ARKit and converted in swift @objc func handleDragGesture(_ gestureRecognizer: UIGestureRecognizer) { let tapPoint = gestureRecognizer.location(in: self.sceneView) switch gestureRecognizer.state { case .began: print("Object began to move") let hitResults = self.sceneView.hitTest(tapPoint, options: nil) if hitResults.isEmpty { return } let hitResult =