|
Huckleduck posted:Quick question about using Swift as a script. I made an XCode project file with the Swift Package Manager. The purpose of the script is to contact a server and download some files, then write those files to disk. This is an async operation, but scripts end whenever they let go of the main thread. I've tried three things, one of which worked. For a classic command line tool with a main run loop, you'd run it repeatedly until the work is done, which would likely be signaled by a variable being set (in a thread-safe manner) by whatever the worker is, and then stopping the main run loop. The main run loop would check the variable (also in a thread-safe manner, of course) and then when it's set decide to stop running. Note that you can't rely on a run loop to stop on its own; the frameworks can add their own sources too, after all. I strongly suspect that whatever mechanism you used to do the download relied on having the main run loop turning, in which case the above sounds like what you should do too. Your #3 is very close to it, you just need to set the Boolean safely, check it safely, actually run the main run loop, and explicitly stop the main run loop after setting the Boolean. This should all also be easy to encapsulate.
|
# ? Apr 10, 2017 09:49 |
|
|
# ? May 5, 2024 07:59 |
|
Worth adding that, IIRC, background queues get to run before dispatch_main() is called, but the main thread does not, until libdispatch is entered via dispatch_main, CFRunLoopRun, or {UI,NS}ApplicationMain. I think the pure libdispatch method is to use dispatch_main() and atexit() for cleanup, and call exit() with your exit code (correct me if I'm wrong). Depending on your script, you can also make things a little nicer if you like by using dispatch sources to respond to signals, accept stdin input, or track file updates, and other cool stuff. You can make it so that cancelling a dispatch source triggers said call to exit(). An empty while loop can easily consume tons of unnecessary CPU cycles, so if it fits the use-case, you can have a pretty low overhead process by using the loop functions that already exist.
|
# ? Apr 10, 2017 10:14 |
|
Moogs posted:So this is a stupid noob question, but I'm a stupid noob... when I'm making an iMessage extension, how do I control what it looks like when it's half screen (like when you have Messages open and hit the app button to switch to it) and when it's full screen? I think the answer is view controllers, is that right? We made an iMessage application back for the iOS 10 launch: https://itunes.apple.com/us/app/plated-dinner-recipes-ingredients/id954364666?mt=8 Have it operate like any other application, except what would normally be your "UIApplicationDelegate" is the "MSMessagesAppViewController". You'll want to respond to the "willTransition(to presentationStyle: MSMessagesAppPresentationStyle)" and "didTransition(to presentationStyle: MSMessagesAppPresentationStyle)" delegate methods accordingly. And yes, we used a storyboard to handle these flows.
|
# ? Apr 10, 2017 15:09 |
|
Huckleduck posted:Quick question about using Swift as a script. I made an XCode project file with the Swift Package Manager. The purpose of the script is to contact a server and download some files, then write those files to disk. This is an async operation, but scripts end whenever they let go of the main thread. I've tried three things, one of which worked. As others have said, you need to run the main runloop. Create a dispatch source signal handler for SIGINT so if the user presses CTRL+C you can cleanup properly. You don't need to bother freeing memory or anything fancy because the process will die shortly anyway, but it is nice to remove any temporary files, etc. code:
|
# ? Apr 10, 2017 20:49 |
|
Thanks for the tips.
|
# ? Apr 12, 2017 17:38 |
|
Is it just me or are Playgrounds slow / sometimes completely broken in 8.3.1?
|
# ? Apr 12, 2017 23:09 |
|
Rudest Buddhist posted:Is it just me or are Playgrounds slow / sometimes completely broken in 8.3.1? Playgrounds haven't had inline errors for me for several releases. The code/run/output cycle isn't any faster these days than running an app/tests. Maybe I need to nuke my Xcode.
|
# ? Apr 13, 2017 01:10 |
|
I got hit with the no inline errors thing on this last update also. My issue is that after one compile the playground just runs indefinitely without giving me any output. I can even change the entire thing to 'let words = "hello"; print(words)' and I get nothing back. Running manually doesn't help either. I guess I'll file a radar, but it's pretty broad. I have other XCode windows open (with full apps) while I'm in the playground. Maybe that's part of it...
|
# ? Apr 13, 2017 01:33 |
|
Kallikrates posted:Playgrounds haven't had inline errors for me for several releases. The code/run/output cycle isn't any faster these days than running an app/tests. Maybe I need to nuke my Xcode. Reinstalling Xcode is virtually never going to help anything. (Same with reinstalling the operating system.) Why is it even on peoples' list of things to try? You'd be better off trying the same thing under a brand new user account, in case there's some cached state interfering.
|
# ? Apr 13, 2017 19:22 |
|
I didn't really uninstall Xcode. It was just a hot take about trying to get Xcode into some working known default state. I generally have pretty good Swift code completion and highlighting compared to most people I talk to that write swift daily. I am just very surprised at how Playgrounds have regressed.
|
# ? Apr 13, 2017 22:15 |
|
I didn't realize how much I like and use playgrounds until they took a dump on me with this last update
|
# ? Apr 13, 2017 23:27 |
|
Is there an elegant abstraction for me to wrap lines of code to check errors in debug mode that will be compiled out zero cost in release mode? Specifically I'm using a rather stateful API with a geterror function that returns an error code, and I want to wrap every call to that API with something that will check for and log errors in debug mode, but get compiled out. I currently have something like this: Swift code:
|
# ? Jun 3, 2017 22:08 |
|
Swift code:
|
# ? Jun 4, 2017 07:12 |
|
Why is it so hard to turn an array of strings into a char** in Swift? :/
|
# ? Jun 7, 2017 00:34 |
|
Swift 4 trip report: updating 10k lines of Swift to compile without warnings as Swift 3.0, 3.1, 3.2 and 4.0 took about 4 hours and involves 12 #if swift(...) checks, most of which are for XCTest changes rather than Swift itself. Definitely the least painful migration by a huge margin (and the first time we've been able to reasonably support two major versions on a single code base).
|
# ? Jun 7, 2017 00:53 |
|
Should I be able to replace Cocoapods/Carthage with Swift Package Manager once Xcode 9 is released? I haven't looked into SPM in awhile, last I remember it didn't work with iOS at all and maybe not with Objective-C laden libraries.
|
# ? Jun 14, 2017 22:08 |
|
SPM 4 still only supports macOS. The last time I poked it at it, it appeared that being able to set the C++ version was the only hard blocker for us being able to support a spm package so I would expect most obj-c libraries to work in theory.
|
# ? Jun 14, 2017 22:44 |
|
Glimm posted:I haven't looked into SPM in awhile, last I remember it didn't work with iOS at all I...have missed that every time I've looked at it. That's an utter deal breaker. Are there plans to make SPM the sort of thing that can actually replace everything else? Carthage, after all, must be destroyed.
|
# ? Jun 15, 2017 00:16 |
|
Everything I can think of is on the roadmap with the possible exception of iOS support, which might not be an explicit goal but certainly seems to be am implicit goal.
|
# ? Jun 15, 2017 03:17 |
|
Uhh, why?
|
# ? Jun 15, 2017 15:03 |
|
Doh004 posted:Uhh, why? I just meant I don't remember seeing anyone say "SPM will for sure support iOS and it'll happen on this date". But I do remember seeing more broad "we want to support all platforms where Swift officially runs" statements.
|
# ? Jun 15, 2017 16:36 |
|
This requires a lot of optimism, building iOS apps is much more than a compiler and dependencies. See recent kerfuffle about swift-stdlib-tool none the less packaging.
|
# ? Jun 15, 2017 16:38 |
|
I always assumed that iOS support was on the roadmap once they had all the core functionality working because no one actually writes macOS applications anymore so why would you waste time building a macOS-only package manager? I guess if the actual goal is to use it to build macOS itself (and the bundled applications) it'd make sense.
|
# ? Jun 15, 2017 16:49 |
|
My understanding is that the initial goal has been to support server-side Swift, primarily on Linux. I’m certainly looking forward to the day it can be properly used for apps though.
|
# ? Jun 15, 2017 21:25 |
|
I'm very new to development, so I'm probably using some wrong terms here, but I'm am trying to make an iMessage app with essentially three separate views -- compact (first image, when App is as big as they keyboard), expanded (second image) and a different view when the user touches a message (third image). I'm trying to change didBecomeActive to account for the possible states... is this the right approach? Am I making any drat sense? code:
|
# ? Jun 18, 2017 01:02 |
|
That's more of a UIKit framework question than a Swift language or tooling question, you might get more assistance in the Apple platform thread.
|
# ? Jun 18, 2017 06:40 |
|
eschaton posted:That's more of a UIKit framework question than a Swift language or tooling question, you might get more assistance in the Apple platform thread. That thread is over here, FYI: https://forums.somethingawful.com/showthread.php?threadid=3400187&pagenumber=288#lastpost
|
# ? Jun 19, 2017 17:27 |
|
eschaton posted:That's more of a UIKit framework question than a Swift language or tooling question, you might get more assistance in the Apple platform thread. ulmont posted:That thread is over here, FYI: Thanks guys, I'll take my question over there!
|
# ? Jun 20, 2017 18:55 |
|
So is Chris Lattner gonna go back to Apple??
|
# ? Jun 22, 2017 06:13 |
|
*crosses fingers* I hope he goes to Facebook.
|
# ? Jun 22, 2017 08:23 |
|
Every time I try to do something with generics I end up wishing I was writing C++. Protocol constraints are nice in theory but I seem to inevitably waste hours trying to work around limitations of the type system. Today's annoyance is that optionals of equatable types have operator == but are not equatable because there's no conditional protocol conformance.
|
# ? Jun 29, 2017 00:46 |
|
Was there a technical tradeoff that the Swift team decided to take in not supporting that, or is it more of a "haven't gotten around to it/didn't think of it" type thing?
|
# ? Jun 29, 2017 01:01 |
|
I think it's in the generics manifesto, so it's a "the design we picked is really hard and will take years to implement" thing.
|
# ? Jun 29, 2017 01:13 |
|
It's "conditional conformances". We intended to implement it in Swift 4 and didn't quite finish. It'll probably be in 4.1.
|
# ? Jun 29, 2017 01:48 |
|
I'm a Swift idiot and a functional programming idiot. For the following, how would I use map or flatmap to print a list of all the cities in "usa"? My apologies but I can't seem to figure it out. code:
|
# ? Jul 5, 2017 22:14 |
|
Haven't tried it butSwift code:
|
# ? Jul 5, 2017 22:38 |
|
Also stop making things optional for no reason.
|
# ? Jul 5, 2017 23:28 |
|
pokeyman posted:Haven't tried it but rjmccall posted:Also stop making things optional for no reason. Yeah, all this optional stuff is making it difficult. This is just an abstraction of a problem I'm facing where the model objects contain obj-c NSArrays. Would it be advisable then to not use .map and .flatmap functions when the data originates from obj-c and is nullable?
|
# ? Jul 5, 2017 23:50 |
|
TheReverend posted:Yeah, all this optional stuff is making it difficult. Coming from objc isn’t really the problem if the properties are helpfully modelled and annotated. It’s quite possible that juggling optional arrays makes flatMap too painful. Partly this is due to both Array.flatMap and Optional.flatMap existing, which can confuse you the programmer and occasionally the compiler too. There’s no shame in good ol' for loops if that’s the clearest way to model your problem. I like map and flatMap but it’s just one tool of many.
|
# ? Jul 6, 2017 00:19 |
|
|
# ? May 5, 2024 07:59 |
|
Yeah, you can still do it with optionals, it just turns it from usa.states.flatMap { $0.cities } into something a little more complex/obscure. But you should also be able to just declare the properties to be nonnull. IIRC, if you do that the bridging logic will automatically turn an unexpected nil return into an empty array.
|
# ? Jul 6, 2017 17:30 |