2018-10-22         Basil

Linear regression result not as expected, following simple test

I have a function, borrowed from Ray Wenderlich, for linear regression across a series of points:extension Array where Element == CGFloat { // A closed form solution fileprivate var average: CGFloat { return self.reduce(0, +) / CGFloat(self.count) }}extension CGFloat { fileprivate static func multiply(_ a: [CGFloat], _ b: [CGFloat]) -> [CGFloat] { return zip(a,b).map(*) } static func linearRegression(a: [CGFloat], b: [CGFloat]) -> (_ a: CGFloat) -> CGFloat { let sum1 = CGFloat.multiply(b, a).average - a.average * b.average ...

 swift                     1 answers                     39 view
 2018-10-22         Les

Swift: casting un-constrained generic type to generic type that confirms to Decodable

SituationI have a two generic classes which will fetch data either from api and database, lets say APIDataSource<I, O> and DBDataSource<I, O> respectivelyI will inject any of two class in view-model when creating it and view-model will use that class to fetch data it needed. I want view-model to work exactly same with both class. So I don't want different generic constraints for the classes// sudo codeViewModel(APIDataSource <InputModel, ResponseModel>(...))// I want to change the datasource in future likeViewModel(DBDataSource <InputModel, ResponseMode...

 swift                     2 answers                     65 view
 2018-10-22         Eve

How to get the realy fixed Device-ID in swift?

I use the below code since long time. I have thought it is uniqueBut I have deleted my app and reinstalled it, I get new different Device-ID !!!if let uuid = UIDevice.current.identifierForVendor?.uuidString {print(uuid)}every new reinstall, i get new ID !I want something which stays the same. Access to the unique device id (UDID) has been disallowed for ages now. identifierForVendor is its replacement, and its behaviour has always been documented. [XXX]Since the value returned from identifierForVendor can be cleared when deleting the app or reset if the user resets it...

 swift                     2 answers                     102 view
 2018-10-22         Gordon

Swift: Check if iOS is connected to wifi without internet

I need to check if an iPhone is connected to a wifi network that won't provide an internet connection. As far as I can see, Reachability et al check for an internet connection, which is not what I need.The device I am connecting to is a proprietary device that has no internet functionality. I need to know whether I am connected to it or not via wifi. // MARK: - Network Checkerfunc isConnectedToNetwork() -> Bool { var zeroAddress = sockaddr_in() zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) ...

 swift                     1 answers                     30 view
 2018-10-22         Eudora

how to upload pdf and image(any one whichever selected) file picked from phone via alamofire in swift

This is the function calling on selecting the file. The parameters needs to be send. And the request url is the server url. func Upload() { var request = URLRequest(url: URL(string: Constants.mainApi + Constants.ADD_CLAIM_DOC)!) request.httpMethod = "POST" request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") request.setValue("Bearer \(userToken)", forHTTPHeaderField: "Authorization") let parameters = ["FileName": myURL as AnyObject, "ClaimID"...

 swift                     1 answers                     77 view
 2018-10-22         Adolph

How to write do {} catch {} in Xcode unit test with 100% code coverage

I'm relatively new to unit testing and I'm hoping to achieve as close to 100% code coverage as possible, the do {} catch {} pattern is giving me grief trying to reach that goal.Given this piece of code:func testUrlRequest_WithAuthenticationNoToken_ExpectingAuthenticationFailure() { let mockController = MockAuthenticationController() mockController.token = nil Server.authenticationController = mockController do { _ = try Server.urlRequestWithHeaders(to: arbitraryEndpoint, excludeBearerToken: false) XCTFail("Expected throw whe...

 swift                     1 answers                     78 view
 2018-10-22         Emma

Binary operator '+=' cannot be applied to operands of type.

I'm practicing some code, some problem brought up.Here's the codeclass Food { let name:String var fruits:[Fruits]? init(name:String) { self.name = name }}class Fruits { let name:String let capacity:Int init(name:String, capacity:Int) { self.name = name self.capacity = capacity }}var macFood: Food = Food(name: "macFood")macFood.fruitsmacFood.fruits = [Fruits(name: "Apple", capacity: 1), Fruits(name: "Banana", capacity: 2)]macFoodmacFood.fruitsmacFood.fruits += [Fruits(name: "Grape", capacity: 3)]All codes were good, printed well, but the ...

 swift                     3 answers                     86 view
 2018-10-22         Noel

Two Table Views in One VC

I want to display two (or more) table views from a single view controller. I mean display simultaneously, e.g., beside each other or one above the other. The only way I can think of doing it is with child view controllers. Is there a better or easier way?Thanks for your comments. Yes of course you can use like this: override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == firstTableView { let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell1", for: indexPath) c...

 swift                     2 answers                     95 view
 2018-10-22         Camille

add a text to navigation bar item

I need to write a number on top of one of my navigation bar items ( on the shopping cart picture)I tried to connect it with an outlet and using this code but it didn't work. self.BtnSabadNav.title = "3"I also tried this code but it didn't work too.self.navigationItem.leftBarButtonItem?.title = "fdf"how should I do this? You would need to have a custom UIView which would contain a UIImage and UILabel. Then you need to set the custom view to the UIBarBarButtonItem.let barButtonItem = UIBarButtonItem(customView: yourCustomView)self.navigationItem.leftBarButtonItem = barBut...

 swift                     1 answers                     100 view
 2018-10-22         Valerie

Swift 4 UILabels delay loadiv user profile

Been swifting for less than a week now so be gentle. Below you can see that if a user is logged into via email or FB, they get a protected page, Main. This page has the user profile. For now it has 2 labels, and 2 buttons to logout and FB share.Whenever this page loads my labels are not loaded w the view, instead there's a delay. Through deduction/testing, it looks to be w the instaniate process. Whatust I do to eliminate the delay with UILabels showing in Main?appcontainerviewcontrollerimport UIKitclass AppContainerViewController: UIViewController { override func view...

 swift                     1 answers                     79 view
 2018-10-22         Solomon

Swift 3 Alamofire Request Progress

I have this kind of codelet req = Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) req.downloadProgress { (progress) in print("progess!", Float(req.progress.fractionCompleted)) } req.responseJSON { response in if response.result.isSuccess { print(response.result.value!) } else { print(response.result.error!) } }The result doesn't give me a progress. it just returns 1.0, that's it, but I need the progress of this request.Please somebody help me....

 swift                     2 answers                     70 view
 2018-10-22         Theresa

How to build Insert Remove Form with Xcode Object Library for Cocoa Swift?

Rocket for Mac has a Preferences Window in which it has a form like below to insert & remove an app.I want to make a form like that but how should I build it? I am new to Cocoa development so this might be obvious.I want to make the one with the label Disable Rocket within these apps: I found a solution.What I did was use NSTableView with single column for the textbox. And made the NSTextField in it to be text editable using the Behaviour attribute.Then for the + & - button I used 2 NSPopupButton & made the Image attribute to be NSAddTemplate & NSRemoveTe...

 swift                     1 answers                     80 view
 2018-10-22         Leila

Using a struct in 2 tableViews (A tabelView within another tabelView)

I have an interesting question. I have a two table views, one inside the other. I have a struct that I want to use to add data to my table views. The struct has 2 string items and an array of another struct that has 3 string items. Simple stuff.The first struct has a band name, its location and an array of its members.the second struct had member first name, last name and role.In the first table view I want to pass the bands name in the label.In the second table view I want to pass all the members in each row.I have no problem with the first table view, but I am completely...

 swift                     1 answers                     79 view
 2018-10-22         Winni

Read from a serial port in Swift 4 using ORSSerialPort

I've been wanting to make an app that sends instructions over serial to my LED controller. For this to work, I need to read what the controller sends back after sending it a command. I found the following function in ORSSerialPort: func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) { // Do things}However, is there something like ORSSerialPort.read()? I don't think ORSSerialPort.read() is a good idea. I know some other serial libraries are written that way, but the only way for that to work is for read() to block (possibly with a timeout) until a byte ...

 swift                     1 answers                     83 view
 2018-10-22         Abraham

Can I use Swift 5 to build targets that are written in Swift 3

In early 2019 or late 2018 Apple will release Xcode 10.x with Swift 5. In my project I use a Cocoa pods which are written in Swift 3 and abandoned by the author.I would like to know if I have to replace the pods or upgrade or maybe I can leave them as they are. If Swift 5 will build targets written in Swift 3, I can continue to use pods written in Swift 3 as they are, if not, I how to upgrade/change it.Question:Does Swift 5 build targets that are written in either Swift 4 or Swift 3? It’s not clear yet how far they’ll go. Source Stability Similar to Swift 4 , the Swi...

 swift                     1 answers                     17 view
 2018-10-22         Grover

Tracking multiple images and play their videos in ARkit2

I've got below sample code, tracking a single image then plays a video using Apple's ARkit.Currently videos stored locally on the device.The code below just tracks a single image and plays corresponding video.How can I modify it and let it track multiple images and play their videos? @IBOutlet var sceneView: ARSCNView!// Create video playerlet videoPlayer: AVPlayer = { // Load cat video from bundle guard let url = Bundle.main.url(forResource: "nyan-cat", withExtension: "mp4", subdirectory: "art.scnassets") else { print("Could not find video file") return...

 swift                     2 answers                     23 view

Page 1 of 656  |  Show More Pages:  Top Prev Next Last