<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stiefels.net &#187; cocoa</title>
	<atom:link href="http://www.stiefels.net/tag/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stiefels.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 20 Sep 2009 10:39:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Syntax Highlighting for NSTextView with flex</title>
		<link>http://www.stiefels.net/2008/01/22/syntax-highlighting-for-nstextview-with-flex/</link>
		<comments>http://www.stiefels.net/2008/01/22/syntax-highlighting-for-nstextview-with-flex/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 20:20:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[Syntax Highlighting]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2008/01/22/syntax-highlighting-for-nstextview-with-flex/</guid>
		<description><![CDATA[Implementing syntax highlighting can be a painful task. Lots of regular expressions and thinking may be required to master this.
On the search for an easy way to implement a syntax highlighting mechanism for NSTextView I found an easy and fast (regarding runtime) way to do this. By using flex (a tool with its roots in [...]]]></description>
			<content:encoded><![CDATA[<p>Implementing syntax highlighting can be a painful task. Lots of regular expressions and thinking may be required to master this.</p>
<p>On the search for an easy way to implement a syntax highlighting mechanism for NSTextView I found an easy and fast (regarding runtime) way to do this. By using flex (a tool with its roots in the 1970's) defining the rules of what should be highlighted makes it a lot more easier and structured.<br />
flex is the GNU version of lex, the lexical analyzer generator (Buzzword jackpot!) developed by Eric Schmidt and Mike Lesk in 1975. By defining some rules (mainly regular expressions) it generates a C program which allows us to scan through a text and do something with the text, e.g. divide it into tokens. Tokens are chunks of characters with a special meaning (as defined in our rules). Confused? Read on, this concept will become very clear soon.</p>
<p>In this article I want to show you how we can use this ancient but very powerful tool to implement a basic syntax highlighting within a NSTextView.<br />
<span id="more-77"></span></p>
<p>## Overview<br />
At first, let's have a look at the goals. In the demo application (see below) we have one single window with a NSTextView where we can write some text. This text is parsed and highlighted in the following way:</p>
<p>* positive real number will be colored <span style="color: #01FF34;">green</span><br />
* negative real numbers will be colored <span style="color: #FF0000;">red</span></p>
<p>A real number can be entered in the following formats:</p>
<p>* 12<br />
* -3.54<br />
* 23e2<br />
* 23E2<br />
* 423.2e-3<br />
* -5e-1</p>
<p>For the regular expression gurus of you the format can be defined like this:</p>
<p>    [-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?</p>
<p>In addition we want to sum up all found real numbers and show the result in our application.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2008/01/capture-reals-screenshot.png" /></p>
<p>I'd recommend you to download the source code and read the comments there for additional information.<br />
This application consists of three parts:</p>
<p>* NSTextView (for the text) and NSTextField (to show the total value),<br />
* the NSTextStorage delegate and<br />
* the actual scanner created with flex</p>
<p>As you can see, we don't need to subclass NSTextView to add highlighting, everything is done in the NSTextStorage delegate and the scanner.</p>
<p>## NSTextView and NSTextField<br />
The NSTextView is only used for the user to input some text. We don't need to change its behavior nor do we need to subclass it. The NSTextField at the bottom is used to show the total of the collected real number. Nothing special here either.</p>
<p>## NSTextStorage delegate<br />
The NSTextStorage delegte is responsible to pass the entered text to the scanner, to retrieve the token information and to add the string attributes (e.g. the color) to the original text.<br />
The main part is a while-loop which fetches all tokens and if the token indicates a real number, an NSForegroundColorAttribute attribute is added to the text. That's all we have to in the delegate in principle.</p>
<p>## The scanner<br />
The scanner is the heart of this implementation. It takes an ordinary C string and parses through it returning tokens if something interesting is found. Actually, it returns tokens for everything it could find (whether it is text or a real number). This behavior is needed in the delegate to determine the range.<br />
To write such a scanner the flex tool becomes VERY handy, because you can define the behavior of the scanner with a fistful rules. Flex takes these rules and generates a more or less large C file (*lex.yy.c*) which is then the actual scanner.<br />
Xcode supports flex files directly by adding a *.l* file to the project. In the build process Xcode runs flex to "translate" it to C code and it also compiles this C code. We don't have to do anything.<br />
I'd suggest you to read the [flex manual](http://www.gnu.org/software/flex/manual/) to get familiar with flex as I won't explain it in detail. Flex is a really cool tool and to deal with it for some time won't harm. <img src='http://www.stiefels.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Unfortunately there is one problem with flex: It doesn't know UTF-8 characters. As you might know UTF-8 characters consist of 2 to 4 bytes and flex thinks every single byte is one character.<br />
But, there is a workaround to this problem. What we can do is to capture all those bytes and return them as on "byte-string". Our delegate will read those bytes in and recognize them as one UTF-8 character.<br />
The designers of UTF-8 were clever and hid the length of the UTF-8 character in the first byte. So, after reading the first UTF-8 byte we can determine how many bytes to follow (more information [here](http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf), page 103). This buffer mechanism requires some additional variables and functions the scanner.</p>
<p>The regular expression mentioned above can easily be split up into more readable parts on the *flex.l* file. Changes to the rules can be applied easily this way.</p>
<p>## Download<br />
The demo application source code can be downloaded via [Git](http://www.git-scm.com):</p>
<p>    git clone git://github.com/simonboots/CaptureReals.git</p>
<p>A universal binary is also available [here](http://www.stiefels.net/wp-content/uploads/2008/01/Capture%20Reals.zip).</p>
<p>## Summary<br />
If you're interested in adding a real fast syntax highlighting to your application, doing this with flex should be one option to be considered. It may not be the perfect solution (UTF-8 workaround, missing runtime flexibility) but it is very easy to implement and may fit your requirements.<br />
I hope this article was at least a little bit helpful. Please let me know what you think about it.<br />
Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2008/01/22/syntax-highlighting-for-nstextview-with-flex/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Late Night Cocoa Podcast</title>
		<link>http://www.stiefels.net/2007/04/09/late-night-cocoa-podcast/</link>
		<comments>http://www.stiefels.net/2007/04/09/late-night-cocoa-podcast/#comments</comments>
		<pubDate>Mon, 09 Apr 2007 08:58:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Podcast]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/04/09/late-night-cocoa-podcast/</guid>
		<description><![CDATA[
[Late Night Cocoa](http://www.latenightcocoa.com) is a great podcast about Cocoa and Mac OS X development and if you're interested in these topics and haven't heard about Late Night Cocoa yet, [fire up your iTunes](http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=213023580)
and give it a try.

Every week the producer Steve "Scotty" Scott invites well known names in the Cocoa community (such as [Aaron Hillegass](http://www.bignerdranch.com), [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.stiefels.net/wp-content/uploads/2007/04/latenightcocoa_logo.png" height="80" width="132" align="left" border="0" style="margin-right:10px;" alt="Late Night Cocoa Logo" title="Late Night Cocoa" /></p>
<p>[Late Night Cocoa](http://www.latenightcocoa.com) is a great podcast about Cocoa and Mac OS X development and if you're interested in these topics and haven't heard about Late Night Cocoa yet, [fire up your iTunes](http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=213023580)<br />
and give it a try.</p>
<p><span id="more-42"></span></p>
<p>Every week the producer Steve "Scotty" Scott invites well known names in the Cocoa community (such as [Aaron Hillegass](http://www.bignerdranch.com), [Scott Stevenson](http://www.theocacao.com/) or [Daniel Jalkut](http://www.red-sweater.com/blog/)) to have a talk about a Cocoa-related topic. It goes from Xcode projects, over CoreData to Memory Management and so on.<br />
It is very interesting and fun to listen to Scotty and his guests and there are a lots of tips on how you can improve your Cocoa development.</p>
<p>For the last three months (that's the time since Late Night Cocoa is on air) Scotty spends about 12 hours a week (!) to produce one episode of this high-quality podcast. So if you like it (as I do), please go to the [Late Night Cocoa website](http://www.latenightcocoa.com) and make a small donation.</p>
<p>Thank you Scotty!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/04/09/late-night-cocoa-podcast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Over 200 Downloads of RegexTester</title>
		<link>http://www.stiefels.net/2007/04/03/over-200-downloads-of-regextester/</link>
		<comments>http://www.stiefels.net/2007/04/03/over-200-downloads-of-regextester/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 18:33:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/04/03/over-200-downloads-of-regextester/</guid>
		<description><![CDATA[I must admit, I am a little surprised that my teeny tiny little software [RegexTester](/projects/regextester) was downloaded over 200 times in just five days (according to [macupdate.com](http://www.macupdate.com/info.php/id/24535)). I am very pleased and I'd like to get some reviews and opinions.  
]]></description>
			<content:encoded><![CDATA[<p>I must admit, I am a little surprised that my teeny tiny little software [RegexTester](/projects/regextester) was downloaded over 200 times in just five days (according to [macupdate.com](http://www.macupdate.com/info.php/id/24535)). I am very pleased and I'd like to get some reviews and opinions. <img src='http://www.stiefels.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/04/03/over-200-downloads-of-regextester/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Little Helper: RegexTester</title>
		<link>http://www.stiefels.net/2007/03/14/new-little-helper-regextester/</link>
		<comments>http://www.stiefels.net/2007/03/14/new-little-helper-regextester/#comments</comments>
		<pubDate>Wed, 14 Mar 2007 20:02:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/03/14/new-little-helper-regextester/</guid>
		<description><![CDATA[A few week ago I [wrote](/2007/01/24/regular-expressions-for-nsstring/) about how the regular expression mechanism in NSPredicate can be used to validate strings.
I created a small application which exactly does that - validating strings.

It's called RegexTester (yeah, innovative name, I know) and is intended to be a help for developers who have to formulate regular expressions.
More information can [...]]]></description>
			<content:encoded><![CDATA[<p>A few week ago I [wrote](/2007/01/24/regular-expressions-for-nsstring/) about how the regular expression mechanism in NSPredicate can be used to validate strings.<br />
I created a small application which exactly does that - validating strings.</p>
<p><img id="image38" src="http://www.stiefels.net/wp-content/uploads/2007/03/regextesterscreen.png" alt="RegexTester Screenshot" /></p>
<p>It's called RegexTester (yeah, innovative name, I know) and is intended to be a help for developers who have to formulate regular expressions.<br />
More information can be found [here](/projects/regextester).</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/03/14/new-little-helper-regextester/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Practice Makes Perfect</title>
		<link>http://www.stiefels.net/2007/02/11/practice-makes-perfect/</link>
		<comments>http://www.stiefels.net/2007/02/11/practice-makes-perfect/#comments</comments>
		<pubDate>Sun, 11 Feb 2007 10:54:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/02/11/practice-makes-perfect/</guid>
		<description><![CDATA[How can you be perfect (or at least good) at something if you don't practice? I think, not at all. Especially when it comes to software development beeing perfect without practice is hardly possible.
So, have some practice! To get some help with that, you should visit [Chris Forsythe's website](http://www.brok3n.org/).

Chris is an [Adium](http://www.adiumx.com) developer and provides [...]]]></description>
			<content:encoded><![CDATA[<p>How can you be perfect (or at least good) at something if you don't practice? I think, not at all. Especially when it comes to software development beeing perfect without practice is hardly possible.<br />
So, have some practice! To get some help with that, you should visit [Chris Forsythe's website](http://www.brok3n.org/).</p>
<p><span id="more-30"></span></p>
<p>Chris is an [Adium](http://www.adiumx.com) developer and provides a great service called [Weekly Cocoa App Challenge](http://brok3n.org/weeklycocoa.html):<br />
Every week Chris posts a small Cocoa application which you have to (try to) re-develop.<br />
After a week Chris pubishes the source code to this application so you can compare your code with the one from the expert.<br />
Practice makes perfect, with Chris' help even easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/02/11/practice-makes-perfect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expressions for NSString</title>
		<link>http://www.stiefels.net/2007/01/24/regular-expressions-for-nsstring/</link>
		<comments>http://www.stiefels.net/2007/01/24/regular-expressions-for-nsstring/#comments</comments>
		<pubDate>Wed, 24 Jan 2007 22:54:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/01/25/regular-expressions-for-nsstring/</guid>
		<description><![CDATA[Last weekend I was looking for different options to validate NSString objects against some regular expressions.
While there are some 3rd party classes for doing regular expressions in Cocoa and wrappers for Perl/PCRE there is a way to do that without external software. The magic word is called [NSPredicate](http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html).
This approach may perhaps not be obvious because [...]]]></description>
			<content:encoded><![CDATA[<p>Last weekend I was looking for different options to validate NSString objects against some regular expressions.<br />
While there are some 3rd party classes for doing regular expressions in Cocoa and wrappers for Perl/PCRE there is a way to do that without external software. The magic word is called [NSPredicate](http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html).<br />
This approach may perhaps not be obvious because there is (at this time) no direct documentation or hint in the [ADC documentation](http://developer.apple.com).</p>
<p><span id="more-28"></span></p>
<p>NSPredicate was introduced in OS X 10.4 and is mainly used in CoreData applications to specify queries and has therefore also some nice functionality to filter arrays.<br />
But there is also a way to use its built-in regular expression mechanism to validate NSStrings.</p>
<p>Let's say you have a string "Hello World!" and you want to check if there are at least two sequent l's in it. The regular expression would be: ".\*l{2,}.\*" (without quotes).<br />
Note, that there is a dot followed by an asterisk (which means "zero or more characters") at the beginning and at the end of the actual expression (l{2,}). This is necessary because (unlike the regular expressions in Perl) the "interpolated length" of the regular expression must be at least the same as the length of the string to test to (I don't know why but that's what I found out during my tests).</p>
<p>In your Cocoa application this test would look like this:</p>
<p>    NSString *mystring = @"Hello World!";<br />
    NSString *regex = @".*l{2,}.*";</p>
<p>    NSPredicate *regextest = [NSPredicate<br />
                             predicateWithFormat:@"SELF MATCHES %@", regex];</p>
<p>    if ([regextest evaluateWithObject:mystring] == YES) {<br />
        NSLog(@"Match!");<br />
    } else {<br />
        NSLog(@"No match!");<br />
    }</p>
<p>This is easy, isn't it?<br />
All you have to do is to create a NSPredicate object with the predicate "SELF MATCHES" followed by a regular expression and call its "evaluateWithObject:" method with your string you want to validate. It returns YES if the string matches your regular expression and NO if it does not.</p>
<p>If you don't like this approach or if you don't want your application to require OS X 10.4 you can find some other solutions and documentation regarding regular expressions in Cocoa at [this webpage](http://www.cocoadev.com/index.pl?RegularExpressions).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/01/24/regular-expressions-for-nsstring/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Weblogs About Mac OS X Development</title>
		<link>http://www.stiefels.net/2006/12/11/weblogs-about-mac-os-x-development/</link>
		<comments>http://www.stiefels.net/2006/12/11/weblogs-about-mac-os-x-development/#comments</comments>
		<pubDate>Mon, 11 Dec 2006 15:29:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[weblogs]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2006/12/11/weblogs-about-mac-os-x-development/</guid>
		<description><![CDATA[If you're a mac developer you certainly subscribed some weblogs about Cocoa development with OS X.
[Scott Stevenson](http://www.theocacao.com) created a nice new website called [Cocoa Blogs](http://www.cocoablogs.com).   It bundles weblogs and tutorials from well known OS X developers.
The place to go for your daily Cocoa dose.
Great idea and very cool design.
Thank you, Scott.
]]></description>
			<content:encoded><![CDATA[<p>If you're a mac developer you certainly subscribed some weblogs about Cocoa development with OS X.<br />
[Scott Stevenson](http://www.theocacao.com) created a nice new website called [Cocoa Blogs](http://www.cocoablogs.com).   It bundles weblogs and tutorials from well known OS X developers.<br />
The place to go for your daily Cocoa dose.</p>
<p>Great idea and very cool design.<br />
Thank you, Scott.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2006/12/11/weblogs-about-mac-os-x-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Code Snippet: STNKeychainAccess</title>
		<link>http://www.stiefels.net/2006/11/30/new-code-snippet-stnkeychainaccess/</link>
		<comments>http://www.stiefels.net/2006/11/30/new-code-snippet-stnkeychainaccess/#comments</comments>
		<pubDate>Thu, 30 Nov 2006 20:54:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[keychain]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2006/11/30/new-code-snippet-stnkeychainaccess/</guid>
		<description><![CDATA[A few minutes ago I updated the [projects page](/projects).
I added a subcategory *Little Helpers* in which I will publish some code snippets for different purposes.
The first one is an Objective-C class for accessing the keychain service in Mac OS X: [STNKeychainAccess](/projects/stnkeychainaccess).
In the downloadable zip-file you can find a Xcode project with a small cocoa application [...]]]></description>
			<content:encoded><![CDATA[<p>A few minutes ago I updated the [projects page](/projects).<br />
I added a subcategory *Little Helpers* in which I will publish some code snippets for different purposes.</p>
<p>The first one is an Objective-C class for accessing the keychain service in Mac OS X: [STNKeychainAccess](/projects/stnkeychainaccess).</p>
<p>In the downloadable zip-file you can find a Xcode project with a small cocoa application which shows how this class can be used in your application.</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2006/11/30/new-code-snippet-stnkeychainaccess/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
