Help with a Swift OS X program

Erik Price erikprice at gmail.com
Tue Jun 2 11:01:00 EDT 2015


Hi David,

I've taken a look at the code you posted. You're right, it uses a very
procedural approach, where the program starts at the top and continues to
the bottom, much like a script. However, Cocoa programs seldom have this
kind of structure. If you take a look at any of Apple's sample code, you'll
see that most Mac programs actually consist of a number of functions, and
often multiple classes. You might want to read through this Apple
documentation introducing Mac app development, to get a sense for it:

https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/chapters/01_Introduction.html

What I'd suggest is to create a brand new project (don't throw the current
code away, since you can reuse most of it) and just make an app that shows
a basic UI with an "open" button. Get it so that clicking the button opens
the NSOpenPanel, then add another button for saving the output file and do
the same thing with NSSavePanel. Once you have this basic skeleton in
place, add the code to create a very basic NSURLSession (perhaps one that
just requests the Google home page or something), and try setting a
delegate or completion block on the NSURLSession. The delegate or block
could just use NSLog to say "success" or something like that.

Once you have those things working, gradually start migrating the code from
your current project into the appropriate functions in the new project,
until you have things working the way you want them to.

I won't be at the meeting on Wednesday, so I apologize that I won't be able
to help you in person. Perhaps there will be others who can. You can
definitely get help for things like this at the Boston CocoaHeads meetings
too (they meet second Thursday of every month). Or feel free to continue
posting here if you have questions or run into any problems making progress.

Good luck

e

On Mon, Jun 1, 2015 at 12:18 PM, David M Rosenberg <rosenberg at acm.org>
wrote:

> Hi Erik,
>
> Thank you for contacting me. So far you are the only one who has, so I’m
> very eager to be in touch with you.
>
> I’ve taken the current source code and shortened it by replacing a very
> long string literal with a short (junk) one, deleting most of the entries
> in a dictionary literal, and replacing the real URL with a fake one. I
> posted the source code at http://pastebin.com/HwNLxbi9  The program
> contains comments about the things that aren’t implemented yet. I haven’t
> implemented a user interface. (It uses NSOpenPanel and NSSavePanel to allow
> the user select the input and output files).
>
> Most immediately, I need help restructuring the use of NSURLSession as
> described in your message. Other issues will be figuring out why
> NSFileHandle is returning nil instead of a handle, setting up a window and
> updating the displayed counts of records processed in the window, and
> responding to clicks on two buttons (one of which I’d like to have change
> its name from Start to Pause to Resume, depending on what state the program
> is in and the other of which I’d like to have change its name from Quit to
> Finish, depending on what state the program is in).
>
> Do you expect to be at the MacTech group meeting this Wednesday? If so,
> could we talk then?
>
> Thanks very much for your response.
>
> /David
>
> > On Jun 1, 2015, at 10:50 AM, Erik Price <erikprice at gmail.com> wrote:
> > David, please disregard this if you've already gotten some help or
> solved your problem.
> >
> > Speaking for myself, and possibly for others, it's easier to evaluate
> whether I can help you if I can see the code you have so far. Do you (or
> can you) post that code to a public source code repository somewhere?
> >
> > At a high level, you are correct that NSURLSession is not designed to be
> used synchronously, and if you've written your code with the assumption
> that it works that way, you'll want to go back and rework things.
> Typically, you would either create a completion block or a class that
> conforms to the NSURLSessionDelegate protocol, and the NSURLSession will
> call you back with either of these facilities to let you know when data has
> been received. At that point, you can buffer the data in memory or write it
> to the output file.
> >
> > Good luck!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mactechgroup.org/pipermail/mactechgroup-discuss/attachments/20150602/3b277b92/attachment-0001.html>


More information about the MacTechGroup-discuss mailing list