[Solved] Unable to convert this Swift 2 code to Swift 3

That line should have failed under any version of Swift for being so unwieldily. First off, safely unwrap the optional. And only use one cast at the end. func isLight() -> Bool { if let components = self.cgColor.components { let brightness = CGFloat((components[0] * 299 + components[1] * 587 + components[2] * 114) / 1000) … Read more

[Solved] How to get data from php as JSON and display on label swift [closed]

What you need to do is use JSONSerialization to get your JSON object from data. Also in Swift 3 use URLRequest and URL instead of NSMutableURLRequest and NSURL. func requestPost () { var request = URLRequest(url: URL(string: “https://oakkohost.000webhostapp.com/test.php”)!) request.httpMethod = “POST” let postString = “numQue” request.httpBody = postString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request) { … Read more

[Solved] Fetch all children from database

This answer is similar to a prior answer here What you want to do is to treat each child in Customers as a DataSnapshot, then the children can be accessed. Given a Firebase structure: usersDatabase uid_0 Customers Ben Smith data: “Some data” Tom Cruise data: “Some data” uid_1 Customers Leroy Jenkins data: “Some data” uid_2 … Read more

[Solved] Display Multiple views in Picker

Use a Menu with a inline Picker as its content: Menu { Picker(selection: $fruit, label: EmptyView()) { ForEach(fruits, id: \.self) { fruit in Text(fruit) } } .labelsHidden() .pickerStyle(InlinePickerStyle()) } label: { HStack { Text(“Picked:”) Text(fruit) } } 3 solved Display Multiple views in Picker

[Solved] using for loop for fibonacci series to print values it will print up to 47 values above that it shows error [closed]

Fib(47) is 2,971,215,073. 2,971,215,073 is larger than 231 – 1 … which is the largest 32 bit signed integer. Hence your calculation is overflowing. (In a lot of programming languages, you wouldn’t have gotten an error. You would have just gotten the wrong answer.) How i find the fibonacci series for number 100. You can’t … Read more

[Solved] Swift 4 pop to a view controller : navigation method [closed]

Make sure your controller is in the navigation stack and you can try this code. for controller in self.navigationController!.viewControllers as Array { if controller.isKind(of: SOListScreen .self) { self.navigationController!.popToViewController(controller, animated: true) break } } 1 solved Swift 4 pop to a view controller : navigation method [closed]

[Solved] Swift How can i take data from custom uicollectionviewcell?

Question: “I should take textfield value from the cell. how can I do that?” Answer: You shouldn’t do that. View objects are for displaying information to the user and for collecting input, not for storing data. This is especially important for UICollectionViews and UITableViews, since both of those create and recycle cells as needed as … Read more

[Solved] Async Next Screen Presentation in SwiftUI

You can use a presentation button with a binding. See: https://stackoverflow.com/a/56547016/3716612 struct ContentView: View { @State var showModal = false var body: some View { BindedPresentationButton( showModal: $isSignIn, label: Text(isSignIn ? “SignIn” : “Next”) .font(.headline) .bold() .frame(width: 100) .padding(10) .foregroundColor(.white) .background(Color.blue) .cornerRadius(20), destination: HomeScreen() ) } } 4 solved Async Next Screen Presentation in SwiftUI

[Solved] Swift 4: Array index out of Range

You declared an array of String var stringArray = [String]() Then you appended one element stringArray.append(forss as! String) That means only index 0 is valid stringArray[0] and you get an out-of-range error for stringArray[1] stringArray[2] etc. Note: The question is not particularly related to Swift 4. All versions of Swift exhibit this behavior. 0 solved … Read more

[Solved] How can i use alamofire for post api [duplicate]

First of all you add almofire library into your project then import almofire into your ViewController then below method apply in your button action. func webServiceLogin(isFbLogin:Bool,email:String,password:String) { var parameters:[String:String]? parameters = [“hash”:email as String,”key”:password ] Alamofire.request(“your url”, method: .post, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseJSON { response in hideHud(self.view) switch response.result { case .success: if let … Read more

[Solved] convert from obj-c to swift

You should check out objectivec2swift.net Converting it makes this: import “SherginNavigationTableViewController.h” import “SherginScrollableNavigationBar.h” class SherginNavigationTableViewController { func initWithStyle(style: UITableViewStyle) -> AnyObject { self = super(style: style) if self { // Custom initialization } return self } func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) // SherginScrollableNavigationBar (self.navigationController.navigationBar as SherginScrollableNavigationBar).scrollView = self.tableView self.title = “ScrollableNavigationBar” } func viewDidDisappear(animated: Bool) … Read more

[Solved] How to parse JSON data and eliminate duplicates in Swift?

You can do as var ph = [String]() var newjson = [[String:String]]() for dict in json { if ph.contains(dict[“Phone”]!) { print(“duplicate phone \(dict[“Phone”]!)”) } else { ph.append(dict[“Phone”]!) newjson.append(dict) } } print(newjson) Hare newjson is the new array of dictionary that do not have duplicate phone solved How to parse JSON data and eliminate duplicates in … Read more

[Solved] Display only some digits of an Int [closed]

It depends on your specific requirements. This prints the first two digits of an integer number let intVal = 12345 print(String(intVal).prefix(2)) Output: 12 Another way which only prints certain ones in the number: let intVal = 12345 let acceptableValues = [“1”, “2”] let result = String(intVal).filter { acceptableValues.contains(String($0)) } print(result) Output: 12 solved Display only … Read more

[Solved] willSet didSet syntax? Like is it a real thing? [closed]

If I understand your question correctly, yes Swift has willSet and didSet property observation built in: struct SomeStruct { var someProperty: Int { willSet { print(“Hello, “) } didSet { print(“World!”) } } } class SomeClass { var someProperty: Int { willSet { print(“Hello, “) } didSet { print(“World!”) } } } solved willSet didSet … Read more