Looking For Tutorials

Discussion in 'Mac Programming' started by Severus Snape, Jul 13, 2011.

  1. Severus Snape macrumors newbie

    Jul 13, 2011
    A little about me before the questions come out:

    I'm 40 years old and just recently decided to get into programming for the Mac. I have XCode 3.something installed on my iMac (the version that I got with the iMac discs), and I should let you all know that I just got my Mac about 4 months ago when I finally had enough of my Windows machine.

    I'm a fairly competent programmer to start with. VB is a second language to me, I had a course in C++ 14 years ago in college and had to learn Java on the fly in my first job after college. And it's now been YEARS since I've touched either one. And considering I refuse to use dual boot on my iMac, I have decided to pick up Objective C/Cocoa/XCode and expand my horizons. I have a couple of ideas for stuff I'd like to try and write, but I need to learn how to do it first.

    The question:

    I have several books on order, but I'm getting itchy and want to get started. So I'm looking for some tutorials on specific topics in Objective C and/or Cocoa to help jump-start my learning. I went through the tutorial included with the XCode documentation on the DVD, and that helped get my brain back into a semi-C-like state, and I can follow what happened and why.

    I'm not talking about the basic tutorials like loops or function calls. Specifically, the tutorials for Objective C I'm looking for include:

    1. Singletons. I have read that this is really the only way to create and use global variables, and some of the programs I want to try and write rely on global variables. If #DEFINE works better than singletons, then a good tutorial on that would be nice.

    2. File I/O. I'd like to be able to read and write to files, especially files that are delimited with commas or pipe ("|") characters.

    3. Database access. I know about the Data Model containers in XCode, but from what I've read (and I haven't read a whole lot on these) they appear to take in data from the user while the program is running instead of accessing an external database file. Personally, I'd prefer to use file i/o, but I'd like a good knowledge of how to do this as well.

    I've searched the internet, and I just can't seem to find anything on these topics in a tutorial format other than "Here's what I did and works, so just copy and paste the code". I don't want those because I'll never learn WHY something works, and I won't be able to alter it for my own purposes.

    I'd also like to find some tutorials on Interface Builder and/or XCode as I'm used to Visual Studio (read: VB), and the design aspect is quite different than what I'm used to.

    I can wait for the books I have on order if necessary, but I figured I would at least give it a shot to see if anyone has any tutorial suggestions for me.

    Thanks in advance for any advice you can give me!
  2. chown33 macrumors 604

    Aug 9, 2009
    There's no particular reason not to use global variables in Cocoa or Objective-C. That is, the reasons are the same as the reasons would be for any other language. Where did you read that it's otherwise? Please be specific; post a URL or name a book.

    Info on the Singleton pattern in Cocoa can be found with google search terms:
    cocoa singleton

    One such reference:
    Cocoa Fundamentals Guide : Cocoa Design Patterns : Singleton

    A singleton isn't necessarily a global. Nor are globals necessarily Singletons. If you don't know what the general Singleton pattern is, see Wikipedia:

    No problem. You are free to use C's stdio library.

    There are also many Cocoa functions that work with files, such as NSFileManager, NSFileHandle, etc. See the Foundation framework:

    And many other classes also have methods that let you write and load instances to files. For example, see NSDictionary and NSArray whose writeToFile: has a corresponding method that reads from a file.

    Do you mean "text files whose contents is delimited with commas or pipe"
    Or do you mean pipes like in the shell?

    In C, I would use one of the scanf() functions for parsing comma-delimited files. In Cocoa I might use NSScanner.

    SQLite is automatically provided. Is that what you mean?

    There's also an object wrapper for SQlite, called FMDB. Google it.

    I don't know what you mean by "take in data from the user". Where you get the data to put in your database is up to you. I could quite easily build a SQLite database whose tables held data gathered from watching network traffic, monitoring a temperature sensor, or simply recording CPU usage over a period of time. That could all run on a Mac mini running as a server, with no user logged in.

    You'll have to point to specific cases. The question is too general to answer.


    Also, bookmark the following:

    On the last of the above URLs, you'll see a "Getting Started" box. You should read the links contained there.

    Rules of thumb:
    1. Be specific.
    2. Post your code.
    3. Describe what you expected to happen.
    4. Describe what actually happened.

    And always be prepared to answer the question "What have you tried?". When doing so, follow the 4 rules of thumb.
  3. Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    Thanks for the reply. Lengthy, in-depth, and provided a bunch of links that I'll have to look at. However, I think you mis-interpreted the questions I was asking. Examples:

    1. I never stated that my research indicated that global variables could not or should not be used, only that what I've read indicated that singletons were either the only or the best way to accomplish this.

    2. If singletons aren't globals, and globals aren't singletons, then can you post a link to a tutorial regarding the usage of global variables please? I tried the search term "Objective C Global Variables" in google, and the pages returned talk about singletons.

    3. I don't have any code to post with questions about why it doesn't work because I haven't started yet. I'm looking for tutorials so I can get started.

    I do need to provide clarification on some of the things I posted:

    1. Yes, I meant text files that are pipe-delimited.

    2. By "take data in from a user", I mean that the container is created in the code, but then when the program is run you enter the data. Not what I want - I need to create a database external to Objective C (which I am quite familiar with doing) and then access the database programmatically within Objective C to retrieve data stored in the database. Returning recordsets of the data and such.

    Again, I'll look through the links provided, but additional information may be needed.
  4. jiminaus, Jul 13, 2011
    Last edited: Jul 13, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    Objective-C is C with extras. In general if you don't find anything specific to Objective-C, then look for a C solution. Global variables in Objective-C work the same way as global variable in C.

    Having said that, it's very rare (if not never) to genuinely needed global variables. Modern programming practice now consideres global variables to be bad. If you've done your object-oriented analysis and design correctly, you won't need them.

    AFTERTHOUGHT: Global variables are design choice. At the moment you can have little understanding about how your ideas can be designed because you don't know how the Cocoa framework is going to heavy influence your design.
  5. chown33, Jul 13, 2011
    Last edited: Jul 13, 2011

    chown33 macrumors 604

    Aug 9, 2009
    Singletons are neither the only nor the best way.

    Where did you read that they are? Be specific. Provide a URL or the title and author of a book.

    Global variables in Objective-C are declared in exactly the same way they are declared in C. If you don't know C, that won't make sense, but it will be easy to google references for what the declaration of C globals looks like.

    Objective-C is a strict superset of C. So everything you have in C is available to you in Objective-C. All the same language features, same libraries, same restrictions, etc.

    You'd parse them the same way you'd parse any other delimiters.

    You'd build the database file using SQLite, for example. Then you'd put that file in your project, and tell Xcode to copy into your app bundle. You can then open it in your app and read its data.

    How you build the database file is up to you. You'd have to interface with SQLite, though, if that's the database you plan on using.

    You don't have to use SQLite to build your data. There are plenty of ways to put structured data into an app. For example, a directory tree whose sub-dirs have specific names, say A-Z, containing sets of image-files and audio files categorized by filename. That's just a simple example.

    If you want a specific suggestion about putting readable data into an app, you'll have to be more specific about what kind of data you want to put in. There are literally hundreds of possible approaches that would work, and not all of them involve a SQL database.

    What you're asking is essentially a production question:
    How do I produce data representing X in a form that's readable by Y?
    Depending on the values of X and Y, you'll get different answers. The question of how to insert it into your app will generally be the same, though: you add the files to the project as resources, and make sure they get copied into the right place in the app bundle.
  6. wlh99 macrumors 6502

    Feb 7, 2008
    I'm not sure what you mean be "container."

    There is nothing in Objective C that requires or even implies that data must come from a user. It is common to see command line utilities with practicaly no user interface.

    Cocca has classes to deal with databases, files of various types, and any libraries used with C or C++ can be used with Objective C as well. So you have plenty of options, its up to you to determine your requirements and pick one. Using a C or C++ library might be the fastest way for you, since you might already know how to do that.

    If you already know C and C++, learning Objective-C won't take long. Learning all of the Classes available to you with Cocca and how to use them will. My best advise is to learn to use the built-in documentation in X-Code. Open the tutorial project you did, find a Cocca class name (starts with NS), highlight it, right click on it, and find it in the documentation. Read all about it, and all about all the classes used in the project.

    Then do the same for the classes that chown33 suggested.
  7. chown33 macrumors 604

    Aug 9, 2009
    Expanding on my earlier comment about interfacing to SQLite...

    Mac OS X has a bunch of command-line tools. Many of these are installed as part of the baseline OS install. Many more are installed with Xcode and the Developer Tools.

    One of the baseline commands is named 'sqlite3'. It's basically an interpreter of SQLite SQL statements. These statements can be any valid SQL statements for SQLite's dialect and features. For example, you can use DDL statements to create tables, indexes, etc. You can INSERT, DELETE, SELECT, or UPDATE whatever you want. It's all there.

    So if the goal was to create a pre-populated SQLite database file, consisting of some tables, indexes, etc. and some initial data, you could automate all that. You'd simply make a text file containing the statements, then run the 'sqlite3' command with input redirected to the text file. Boom, it builds your database file. You can build it in the right place in your Xcode project using commands run by Xcode, or you can build it outside of Xcode, and simply move the pre-built database file into Xcode's project folder at the right place.

    There are many other commands for operating on data of all kinds:
    plist (property-list) files, text files, images, audio, and many other things.

    A few example commands, all available in baseline OS install:
    sips - scriptable image processing system
    afconvert - convert audio files between various formats
    afplay - play audio files
    say - text-to-speech synthesis (option to write as audio file)

    Xcode itself can run arbitrary commands as part of a build process. The Run Script build phase contains the commands. You write it as a shell script. The simplest arrangement is a single command line. More complex is a series of commands. Even more complex would use the shell's own conditional statements, looping structures, etc. to execute multiple commands, or perform various procedures. The shell is much more capable than a batch file; it's a full-on programming language.

    You also have other commands at your disposal, installed in the baseline. For example, the 'awk' or 'perl' languages excel at processing text files, especially record-oriented ones with delimiters between fields. There are plenty of references on awk and perl, which a google search will turn up.

    You don't have to run commands only in Xcode. You can also type them into Terminal.app, or copy and paste them from a text file into a Terminal window.

    There are builtin man pages for all the commands. The 'man' command will show them. There's also an online reference here:

    Example man page for the 'awk' command:
  8. Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    I was not aware that SQLite is part of the install for either the OS or XCode, so I thank you for pointing that out.

    As much as I like typing, I don't like using the command line or text files to populate database tables. I mean, if I'm typing out all of the values that are to be inserted into a table, then I'd rather just create the table in a visual environment and the directly enter the data into the table myself. Just a preference. Which leads me to...

    Is there a visual editor for SQLite? I know - I should really just google this. But, as I'm a noob at programming for the Mac and have no prior knowledge of SQLite, I'm relying on you guys to help me out here. Any links to documentation on visual editors would be really helpful.

  9. kainjow Moderator emeritus


    Jun 15, 2000
    I use Base (shareware) but there are other free ones (not as nice though IMO). Google "mac sqlite editor" for other ones, and/or go to macupdate.com.
  10. chown33 macrumors 604

    Aug 9, 2009
    Either I google it and post my results, or you google it and find your results directly. Since you're the one deciding on a suitable product, it makes more sense to me if you do your own search and subsequent product evaluation.

    My first search terms:
    mac os x sqlite gui
    mac os x sqlite viewer
    mac os x sqlite database browser

    Coincidentally, those are also google's auto-completion suggestions, so it's not like I had to be a google-fu master.

    If you get poor or no search results, post your google search terms.

    If you get what seem to be useful results, and want to ask about a specific product, then post a specific question.

    This is why I posted the 4 rules of thumb, and said you should be prepared to answer the question "What have you tried?". No one here expects you to be an expert on Mac development. However, we will usually expect you to be able to do basic stuff yourself. If you have problems with the basic stuff, then describe exactly what you did and what you expected (rules of thumb again).
  11. Severus Snape, Jul 14, 2011
    Last edited: Jul 14, 2011

    Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    Using Google (so as you can see, chown, I do know how to perform a google search), I was able to find where the location of SQLite is supposed to be on my Mac (/usr/bin). For some reason, I cannot find this location at the root of the Mac HDD, but I can find this location at /Developer/usr/bin. Yet I cannot find SQLite in this location.

    Am I looking somewhere wrong? Where exactly is SQLite located?
  12. chown33, Jul 14, 2011
    Last edited: Jul 14, 2011

    chown33 macrumors 604

    Aug 9, 2009
    The Finder doesn't show everything. It will hide:
    - files or dirs whose names start with "."
    - files or dirs with the invisible attribute (a bit flag)
    - certain files and dirs located at the root of the volume

    It turns out that /usr/bin is located at the root of the volume, and "/usr" is in the category of Finder's unshown files. It's not the only thing in that category.

    There are command-lines you can execute that tell the Finder to show all files and folders. Google search terms:
    finder show all

    Example result:

    By the way, the actual command name 'sqlite3', not 'SQLite' or 'sqlite'.

    For the record, I didn't question your ability to use google. I was simply wondering whether you had done so or not, and if not, why not. Being a programmer implies a certain level of ability to help oneself. When helplessness is exhibited, I almost always wonder what is preventing helping oneself, especially if the task is easy or common.
  13. Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    2 parts to this response.

    Part 1: I read through the site you posted, and the light bulb went on. "Just like windows not displaying system or hidden folders" I said to myself. And it's cool that there's a way to display these things in finder. I just have an issue with how it's done.

    One of the commands used is killAll, which (because I wanted to know exactly what it did) I read up on. http://guides.macrumors.com/killall The document states that it terminates the process that is named after the killAll statement. Which I get. What I don't get is why it's being used in the example you provided. I mean, if you use killAll Finder, wouldn't that shut down finder? Considering that Finder is always running (at least on my machine), isn't this a bad thing?

    Part 2: I think an explanation on my part is needed here. I do run google searches all the time. The reason I'm asking for help here is because you guys are the experts and I'd rather get your opinions on the best stuff out there up front instead of wasting time weeding through crap and then coming back out here to ask what you guys think.

    Google is great, yes. But it also has a tendency to bring up crap that is either irrelevant or sub-par. By asking for your opinion, I'm trying to save as much time as possible, while getting to the links that I can use to teach myself how to do this stuff.

    Does that make sense?
  14. wlh99 macrumors 6502

    Feb 7, 2008
    When you kill the finder, it will automaticly restart itself. This forces it to reload certain configuration options.
  15. chown33 macrumors 604

    Aug 9, 2009
    You're trying to save your own time, at the expense of everyone else's. Not that that's necessarily bad, just clarifying things.

    First, if you do nothing at all, not even the most basic of searches, then why do you think we'd necessarily do something else? If you have an idea for search terms to use, why don't you try them and see what happens? With as much experience as you said you have in programming, surely this strategy has come up before.

    Trying it and seeing what happens is especially applicable if the task is easy to initiate (entering search terms into a text box), and the results are relatively easy to evaluate. Honestly, how hard is it to spend 2 or 3 or even 5 minutes doing searches and noting results? If you keep a record of your search terms, then you'll be able to answer the question "What have you tried?". If you then describe exactly how the results were ineffective, or the displayed products were inapplicable, we at least have something to go on.

    We can't read your mind. We can't see your screen. We can't know what you've done unless you tell us. If you don't tell us that you performed some searches, and provide at least some idea of the search terms, then we are almost certainly going to start by doing the most obvious thing: basic searches.

    This is why the first rule of thumb I gave is "Be specific." It's because we don't know what you've tried until you tell us. If you tell us nothing useful about what you've done, then we may assume you've done nothing useful. Or we may ask you what you've tried, and please be specific.

    Here's a link about asking questions and getting answers:

    If you never read it before, it will give you some idea of where we're coming from, and what you can do to help us help you.
  16. Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    Valid points, all of them. It is my bad for not indicating that I had run some google searches first and came up empty-handed. My bad.

    However, and with that said, when I ran the google search on "killAll Finder", I got no information as to why it was being used. And I don't like just running something without knowing why and what effect it's going to have. But that's neither here nor there now.

    On the topic of visual editors for sqlite (and yes, I know that the command is sqlite3 - I just like capitalizing where necessary for readability on a forum), I have decided to use Navicat for SQLite. Not that anyone cares, but I'm just letting you know that I found a solution I think I'll be happy with.

    I'm now off to read through my books that have finally arrived. I'm sure I'll have questions.
  17. chown33 macrumors 604

    Aug 9, 2009
    I thought the killall Finder question was perfectly valid. Precisely because they didn't explain that Finder will auto-restart when killed like that.

    FWIW, the Dock is another process that will auto-restart when killed. I mention this because there are some Dock customizations using the command-line that do the same kind of thing: a 'defaults' command, then killall.

    For future readers, perhaps you could summarize the reasons for your choice.
  18. Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    At the request of chown, I am giving my reasons for going with Navicat for SQLite as the visual editor.

    The primary reason is that the main view is a lot like SQL Server 2005/2008, which is what I'm used to. I like the layout and look, with the connection on the left, SQL in the upper right, and results displayed in the lower right. It's familiar to me, and to me familiar = ease of use.

    The second reason is that I'm also used to MS Access and it's ability to have a visual query designer, and Navicat also has that. While I can write SQL by hand, sometimes it can get cumbersome and lengthy, and mistakes can and do happen producing either no results or results that are skewed. Having a visual query designer helps alleviate some of these issues, and again, familiar = ease of use.

    Those are the 2 primary reasons I'm going with Navicat. Once I get it installed (I got side-tracked last night) and have a chance to play around with it, I'll post another review here so people can read what it's like from a noob's perspective.
  19. Severus Snape thread starter macrumors newbie

    Jul 13, 2011
    So I got the books I ordered, and I immediately started reading through "Programming in Objective-C, Third Edition". I cannot believe how much syntax for programming in a C-based language I had forgotten. Granted, it's been quite a while, but still...

    I'm a complete noob doing this Mac programming stuff. But I can honestly say that all noobs should at the very least read this book. Only on chapter 5 (loops), but the brush-up on syntax alone is well worth it. Should have started there before asking questions.
  20. denniscote macrumors newbie

    Jul 19, 2011

Share This Page