Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

lord funk posted:

Yeah good luck. I just went through this a few weeks ago, and was surprised at how difficult heterogenous arrays were to encode / decode.

Heterogeneous collections are always going to be a pain under strict static typing. It essentially demands extraction of protocols and wrapper enums.

Adbot
ADBOT LOVES YOU

brand engager
Mar 23, 2011

We use this for json in a few places https://github.com/SwiftyJSON/SwiftyJSON
Be aware that it recurses through the whole json object though, so if you have really big json arrays and/or a ton of nesting it can get slow. There was one spot where we had a 10000+ array of some json structures and we had to fall back to passing it as [Any] or [[String: Any]] depending on if we needed to access anything in it.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
These are kinda cool https://github.com/marksands/BetterCodable

duck monster
Dec 15, 2004

frogbs posted:

I realize i'm just getting started with Swift, so everything is going to have a learning curve, but i'll be damned if I can't wrap my head around using Codable to parse even moderately complicated/nested JSON.

Codables one of those things that kind of looks gnarly on paper, but in practice turns out to be pretty elegant. Theres a few sites which can just sort of magic up class definitions and stuff to work with defined JS feeds, and studying the output of those can be pretty enlightening. Its really just a way to stuff class heirachies into actual heirachies and vice versa.

Also like most "schemaless" type things, hetrogenous arrays usually mean to me "Rethink this". Get your datastructures right BEFORE starting your code, if at all possible.

Stringent
Dec 22, 2004


image text goes here

duck monster posted:

Also like most "schemaless" type things, hetrogenous arrays usually mean to me "Rethink this". Get your datastructures right BEFORE starting your code, if at all possible.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

brap
Aug 23, 2004

Grimey Drawer
Heterogenous collections can still have schema—unless it’s impossible to declare an enum which describes the contents of the collection?

frogbs
May 5, 2004
Well well well

duck monster posted:

Codables one of those things that kind of looks gnarly on paper, but in practice turns out to be pretty elegant. Theres a few sites which can just sort of magic up class definitions and stuff to work with defined JS feeds, and studying the output of those can be pretty enlightening. Its really just a way to stuff class heirachies into actual heirachies and vice versa.

Also like most "schemaless" type things, hetrogenous arrays usually mean to me "Rethink this". Get your datastructures right BEFORE starting your code, if at all possible.

Yeah, I tried using this https://app.quicktype.io/ with the feed i've been working with (https://api.weather.gov/gridpoints/PQR/110,101/forecast), but run into some syntax errors with the code Quicktype spits out. I'm ging to upgrade to Xcode 12 and see if I have better luck.

I also stumbled across this library for dealing with weather.gov data in Swift today, so i'm going to give that a shot too: https://github.com/WeatherProvider/NationalWeatherService-Swift

For the curious, here's what Quicktype spits out from that weather.gov feed. Does this look like it's handling nested things like 'Period' correctly?:

code:
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let welcome = try? newJSONDecoder().decode(Welcome.self, from: jsonData)

import Foundation

// MARK: - Welcome
struct Welcome: Codable {
    let context: [ContextElement]
    let type: String
    let geometry: Geometry
    let properties: Properties

    enum CodingKeys: String, CodingKey {
        case context = "@context"
        case type, geometry, properties
    }
}

enum ContextElement: Codable {
    case contextClass(ContextClass)
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        if let x = try? container.decode(ContextClass.self) {
            self = .contextClass(x)
            return
        }
        throw DecodingError.typeMismatch(ContextElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ContextElement"))
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .contextClass(let x):
            try container.encode(x)
        case .string(let x):
            try container.encode(x)
        }
    }
}

// MARK: - ContextClass
struct ContextClass: Codable {
    let version: String
    let wx: String
    let geo, unit: String
    let vocab: String

    enum CodingKeys: String, CodingKey {
        case version = "@version"
        case wx, geo, unit
        case vocab = "@vocab"
    }
}

// MARK: - Geometry
struct Geometry: Codable {
    let type: String
    let coordinates: [[[Double]]]
}

// MARK: - Properties
struct Properties: Codable {
    let updated: Date
    let units, forecastGenerator: String
    let generatedAt, updateTime: Date
    let validTimes: String
    let elevation: Elevation
    let periods: [Period]
}

// MARK: - Elevation
struct Elevation: Codable {
    let value: Double
    let unitCode: String
}

// MARK: - Period
struct Period: Codable {
    let number: Int
    let name: String
    let startTime, endTime: Date
    let isDaytime: Bool
    let temperature: Int
    let temperatureUnit: TemperatureUnit
    let temperatureTrend: String?
    let windSpeed, windDirection: String
    let icon: String
    let shortForecast, detailedForecast: String
}

enum TemperatureUnit: String, Codable {
    case f = "F"
}

brand engager
Mar 23, 2011

gently caress xcode 12 why do I even bother updating this poo poo

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Works great over here. My previous non-use of tabs has apparently avoided some pain.

TheReverend
Jun 21, 2005

Uh oh I'm a tabber and haven't updated yet. What's the beef ?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

TheReverend posted:

Uh oh I'm a tabber and haven't updated yet. What's the beef ?

Tabs in your tabs: https://www.jessesquires.com/blog/2020/07/24/how-to-fix-the-incomprehensible-tabs-in-xcode-12/

TheReverend
Jun 21, 2005

That's just recursion. 🤷‍♂️

Hey since we're speaking tabs, is there a way to. It get whatever tab I'm on to be the breakpoint viewer.

Sounds dumb but I'd like it to open.....in a new tab.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

TheReverend posted:

That's just recursion. 🤷‍♂️

Hey since we're speaking tabs, is there a way to. It get whatever tab I'm on to be the breakpoint viewer.

Sounds dumb but I'd like it to open.....in a new tab.

Same blog has a post that might help! https://www.jessesquires.com/blog/2018/07/01/xcode-tip-debugging-behavior-new-tab/

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?
What we did was add editor tabs like people have been asking for since forever because some people prefer them to the window tabs we added about a decade ago.

We still have window tabs too though since folks have workflows built around them. For example I have behaviors set up to create window tabs for search, debugging, build results, and test results.

KidDynamite
Feb 11, 2005

anyone have a good way to do a bottom half modal?

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!
I'm trying to do this official SwiftUI tutorial which I found linked from the Xcode 12 website but this tutorial is for Xcode 11 and mentions things like "Single View App" which I don't see in Xcode 12. I'm sure it doesn't matter too much but do they have an up to date Xcode 12 SwiftUI tutorial or is there a better one elsewhere? Just seems kinda silly that Apple has a "what's new in Xcode 12" website but haven't updated their tutorials to actually use Xcode 12.

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?
We retired a bunch of templates in Xcode 12, consolidating on App and Document App templates to simplify things for developers. What you get with the App template is essentially what you got from the Single-View App template in Xcode 11.

To most closely match Xcode 11, select the App template and (if presented with the choice) choose “UIKit/AppKit App Delegate” for the Life Cycle option.

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!

eschaton posted:

We retired a bunch of templates in Xcode 12, consolidating on App and Document App templates to simplify things for developers. What you get with the App template is essentially what you got from the Single-View App template in Xcode 11.

To most closely match Xcode 11, select the App template and (if presented with the choice) choose “UIKit/AppKit App Delegate” for the Life Cycle option.

Oh, alright, thanks. Other than some minor things like the live preview button being different it hasn't matter much.

e: Ok so I'm on a 13" MBP and I'm trying to look at both Safari and Xcode at the same time and it's virtually impossible. In Xcode I've hidden everything but the editor but I can't size it down (horizontally) past some arbitrary limit. On the Safari side I can make the window smaller horizontally but at some point it stops letting me resize it and the two windows overlap. Also the full screen split screen doesn't seem to work with Xcode and Safari?

Boris Galerkin fucked around with this message at 00:52 on Nov 5, 2020

SaTaMaS
Apr 18, 2003

KidDynamite posted:

anyone have a good way to do a bottom half modal?

use a UIPresentationController

SaTaMaS
Apr 18, 2003
What do Swift developers think of this? https://www.iosapptemplates.com/blog/swiftui/swiftui-drawbacks
Accord to this Swift UI isn't yet ready for complex projects. Florian does a lot of good stuff on Swift so I'm inclined to agree.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Nothing I currently work on has a minimum SDK that admits SwiftUI. If I was starting something new for fun then I would absolutely try it. Starting something for work, I wouldn't go whole hog, but I'd wedge it in to a couple screens at least.

brand engager
Mar 23, 2011

We redid ours in swiftUI, it wasn't too difficult by itself. My main annoyance with it is that they did a second iteration to make it complete in ios14, but didnt backport any of that poo poo so we cant use it.

lord funk
Feb 16, 2004

I won't be getting a silicon Mac any time soon, but does this mean that Metal apps will run natively in the Simulator?

brand engager
Mar 23, 2011

lord funk posted:

I won't be getting a silicon Mac any time soon, but does this mean that Metal apps will run natively in the Simulator?

I think it depends on whether it's implemented for simulators. Like when I was doing stuff with NETunnelProvider a while back I could only test it on a real device, because the simulator doesn't implement the lower level stuff for vpns.

Pulcinella
Feb 15, 2019
I thought Metal was already supported with some limitations in the simulator?

https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator

Edit: I understand better what you are asking. I imagine it depends on if Metal on the M1 GPU implements more Metal features than the A14 versions do. I could see an “M1X” or “M1Z” variant in the large MBP, iMac, or Mac Pro implementing more features than are available on the A14 just like the AMD GPUs don’t have the same features as Metal on the A14. So probably not.

Pulcinella fucked around with this message at 01:08 on Nov 18, 2020

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice
We haven't bumped the GPU family if that's what you're wondering. Otherwise it works the same way it does for iOS 13 and later: the Metal jobs are serialized and shipped out to the host GPU to execute.

dc3k
Feb 18, 2003

what.

pokeyman posted:

Nothing I currently work on has a minimum SDK that admits SwiftUI. If I was starting something new for fun then I would absolutely try it. Starting something for work, I wouldn't go whole hog, but I'd wedge it in to a couple screens at least.

I wanted to see how much work it would be to get it into one of our apps and I found that Widgets were a good place to learn some basics.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

dc3k posted:

I wanted to see how much work it would be to get it into one of our apps and I found that Widgets were a good place to learn some basics.

That's a good idea. Now to ponder what a useful widget might look like…

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?
PM inbox count for Awful

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
That could work.

Also makes me realize that Mail offers no widgets.

Fate Accomplice
Nov 30, 2006




I work on an app with a tableview whose data comes from a realm db.

That tableview’s cells display status information about the model objects - the status info is calculated on the model object, and the cells use a lazy var to access that status info and layout the cell.

This is all done on the main thread, so scrolling performance is bad. I’ve been tasked with moving the calculation of that status info to a background thread.

I’m struggling to figure out how to replace a lazily calculated bool property on the cell that tells model object to do the calculation with something that does that calculation off the main thread without a nested nightmare of functions with completion handlers.

Anyone have any top level insight as to how to do this?

prom candy
Dec 16, 2005

Only I may dance
I'm a web developer that's pitching in on an iOS client for our main application. So far I've been working on just adding functions for fetching data from our endpoints which has been pretty easy so far, but I want to learn how to write UI stuff as well. I want to start by adding a relatively simple component to an existing view that we have where a user searches to find their Twitter handle and selects it. After they select an account I want to replace the search field with this:



I have a few questions about how I would do this. My guess is that I want to use a UIStackView for the outer container, with a UIImage for the Twitter logo, a UILabel for the Twitter handle, and a UIButton for the X button (possibly with a UIImage inside it because that little X is an SVG.) Does this sound right?

I'd like to build this programmatically because I've watched some videos of Interface Builder and some videos of people doing AutoLayout programmatically and the latter seems a lot more appealing to me. I also understand that if multiple people are committing to .xib files at once it can cause a real nightmare for merge conflicts. Admittedly I googled stuff like "do I need interface builder" so I think I probably got answers from people who also don't like interface builder.

I'm also not sure where I should put the actual code for this. This is not really a reusable component, it only appears here, but it feels like maybe I should still create a separate file called like SelectedTwitterView that subclasses UIStackView and exposes some way of handling setting the username and dealing with the close button being tapped. Is that normal or would it be best to just put it all inside the view controller for the screen that it's on?

Finally, if I have two views that sort of replace one another (the search field, and this one) is it better to add and remove them as state changes, or just hide and unhide them?

Thanks!

Fate Accomplice
Nov 30, 2006




I’m pretty sure what you’re looking for can be done with a uitextfield - you can get the cancel X for free, and I think it has a left side accessory view you can use for the Twitter icon

prom candy
Dec 16, 2005

Only I may dance
Oh neat, it's not actually a text field though (i.e. you can't type in it and clicking the cancel button shouldn't necessarily clear it) does that matter? It does look a lot like a text field.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
If there's no editing the text then yeah your proposed layout makes sense to me. You might want to double-check the compression resistance and content hugging priorities so e.g. the twitter logo doesn't get squished when someone has a long twitter handle.

UIButton knows how to display an image, so for the X button you can just call setImage(_:for:) and give at least the normal state (ideally also a highlighted state).

And I would absolutely put this in its own class. Could subclass UIStackView, or just subclass UIView and add a stack view as a subview. Up to you if you wanna put it in its own file; if it's only used in one place I like to define it where it's used, so it's easy to find.

Usually I would hide/show (or, visually equivalent but animatable, set alpha to 0 or 1) rather than continuously add/remove subviews. There are times it's better to remove and re-add, but default is hide/show.

Fate Accomplice
Nov 30, 2006




prom candy posted:

Oh neat, it's not actually a text field though (i.e. you can't type in it and clicking the cancel button shouldn't necessarily clear it) does that matter? It does look a lot like a text field.

...now I'm confused. it was a search field before the user typed/tapped search and now it looks like one but isn't necessarily interactive?

looking at the image I am expecting to be able to tap onto the name to search again, or tap the cancel X to clear this search and restore the unsearched view. if that's not the case I think it should look different.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
Oh, yeah, if you're switching between a text field and a mostly-static field then I would just use UITextField. It'll ask its delegate whether it should allow editing and what should happen when tapping the X (I think), so you can prevent editing at certain times if you want.

KidDynamite
Feb 11, 2005

dc3k posted:

I wanted to see how much work it would be to get it into one of our apps and I found that Widgets were a good place to learn some basics.

I wish I had been smart and done this. I built a new e commerce checkout for work with SwiftUI and it was great for the first 90%. Trying to finish out the last 10% which requires integrating with some 3rd party frameworks might kill me. Also the keyboard is trash and it’s very bad for users.

prom candy
Dec 16, 2005

Only I may dance

ketchup vs catsup posted:

...now I'm confused. it was a search field before the user typed/tapped search and now it looks like one but isn't necessarily interactive?

looking at the image I am expecting to be able to tap onto the name to search again, or tap the cancel X to clear this search and restore the unsearched view. if that's not the case I think it should look different.

The problem is we need to not only save the user's twitter handle but also the special little twitter user id, so rather than being able to just type your handle in we need you to use this autocomplete search and select one of the results, which we then pass to the server when you hit a "save" button below. If you come back later on we'd need you to first remove your account by clicking the X, then search and select another one (or save it with nothing selected if you just want to clear your account.) It's not the greatest UX but it's also buried pretty deep in a settings area that doesn't get used too often (which is why I wanted to start here.)

That said it probably would be nice if the design made this look a little less like a text field. Right now when the search is active the left icon is a magnifying glass rather than the twitter logo, but otherwise it really does look like our text field component. That's a good note and I'll bring that back to our designer on Monday. And I think I'll probably build it out as a UIStackView in case it ends up coming back looking nothing like a text field. This is just one of those cases where we get a design, then we get into building it and go "hey we can't actually do that the way you designed it" and then the designer fires back a compromise. The nice thing is I'm pretty familiar with the annoying edges because I already built this whole thing in React for our web app.

pokeyman posted:

And I would absolutely put this in its own class. Could subclass UIStackView, or just subclass UIView and add a stack view as a subview. Up to you if you wanna put it in its own file; if it's only used in one place I like to define it where it's used, so it's easy to find.

So is it cool and good to just chuck this class underneath my ViewController class in the same file? I know in some languages that's not all that popular but it does seem like Xcode makes it a little easier to navigate long files. The more languages I learn the more I find I want to learn the the sort of style/housekeeping practices early on but a lot of tutorials don't really focus on that. The guy who started this project also uses 4 spaces for indenting so I'm suspicious about how much I want to trust his way of doing stuff (while obviously still trying to maintain consistency in the project)

Adbot
ADBOT LOVES YOU

Fate Accomplice
Nov 30, 2006




prom candy posted:

So is it cool and good to just chuck this class underneath my ViewController class in the same file? I know in some languages that's not all that popular but it does seem like Xcode makes it a little easier to navigate long files. The more languages I learn the more I find I want to learn the the sort of style/housekeeping practices early on but a lot of tutorials don't really focus on that. The guy who started this project also uses 4 spaces for indenting so I'm suspicious about how much I want to trust his way of doing stuff (while obviously still trying to maintain consistency in the project)

I would only do this if this particular subclass will never be used elsewhere. if there's a chance of reuse elsewhere in the codebase, make a new file.

re: long files, the last two professional codebases I've worked with have each had linting rules against long files, so take that as you will.

Fate Accomplice fucked around with this message at 19:55 on Nov 21, 2020

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply