Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Site updated at 2013-11-30 09:42:24 UTC

  • Loading branch information...
commit a80b4c76081182596fb06c9e7294ed1d094be3da 1 parent 4df2f75
@TonnyXu authored
View
32 2012/11/21/finished-setting-up-octopress/index.html
@@ -5,7 +5,7 @@
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
- <title>Finished setting up Octopress - Learning Foundation.framework | Tonny Xu</title>
+ <title>Finished setting up Octopress - Tonny Xu</title>
<meta name="author" content="Tonny Xu">
@@ -25,7 +25,7 @@
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
- <link href="/atom.xml" rel="alternate" title="Learning Foundation.framework | Tonny Xu" type="application/atom+xml">
+ <link href="/atom.xml" rel="alternate" title="Tonny Xu" type="application/atom+xml">
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
@@ -48,9 +48,9 @@
<body >
<header role="banner"><hgroup>
- <h1><a href="/">Learning Foundation.framework | Tonny Xu</a></h1>
+ <h1><a href="/">Tonny Xu</a></h1>
- <h2>Life is like a box of chocolates...</h2>
+ <h2>iOS/Mac/Ruby/JS/HTML5 lover. Japan Mobile industry observer</h2>
</hgroup>
@@ -120,7 +120,7 @@ <h1 class="entry-title">Finished Setting Up Octopress</h1>
expressions, read this. Other posts like
<a href="http://akkunchoi.github.com/jekyll-github-blogging.html">akkunchoi</a> who
built his blog system on Jekyll which is the base system of Octopress.
-Another guy <a href="http://blueplanet.github.com/blog/page/8/">BluePlanet</a> moved from Jekyll to Octopress. <a href="http://change-the-world.github.com/blog/2012/08/26/1/">Shun Matsumoto</a> also wrote a post in Japanese. If you&#8217;d like to read Japanese, it&#8217;s a good post.</p>
+Another guy <a href="http://blueplanet.github.com/blog/page/8/">BluePlanet</a> moved from Jekyll to Octopress. <a href="http://change-the-world.github.com/blog/2012/08/26/1/">Shun Matsumoto</a> also wrote a post in Japanese. If you&rsquo;d like to read Japanese, it&rsquo;s a good post.</p>
<p>For me, the reason is simple.</p>
@@ -132,7 +132,7 @@ <h1 class="entry-title">Finished Setting Up Octopress</h1>
</ol>
-<h2>What&#8217;s next?</h2>
+<h2>What&rsquo;s next?</h2>
<p>Just keep on updating my blogs and bring you high quality posts on
iOS/Mac/Web/HTML5 relates fields.</p>
@@ -171,6 +171,8 @@ <h1 class="entry-title">Finished Setting Up Octopress</h1>
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://TonnyXu.github.com/2012/11/21/finished-setting-up-octopress/" data-via="TonnyXu" data-counturl="http://TonnyXu.github.com/2012/11/21/finished-setting-up-octopress/" >Tweet</a>
+ <div class="g-plusone" data-size="medium"></div>
+
<div class="fb-like" data-send="true" data-width="450" data-show-faces="false"></div>
@@ -180,8 +182,6 @@ <h1 class="entry-title">Finished Setting Up Octopress</h1>
<p class="meta">
- <a class="basic-alignment right" href="/2012/12/24/learning-foundation-dot-framework/" title="Next Post: Learning Foundation.framework">Learning Foundation.framework &raquo;</a>
-
</p>
</footer>
</article>
@@ -201,14 +201,6 @@ <h1 class="entry-title">Finished Setting Up Octopress</h1>
<ul id="recent_posts">
<li class="post">
- <a href="/2013/02/10/nsstring-hash-function/">NSString - Hash function</a>
- </li>
-
- <li class="post">
- <a href="/2012/12/24/learning-foundation-dot-framework/">Learning Foundation.framework</a>
- </li>
-
- <li class="post">
<a href="/2012/11/21/finished-setting-up-octopress/">Finished setting up Octopress</a>
</li>
@@ -305,6 +297,14 @@ <h1 class="entry-title">Finished Setting Up Octopress</h1>
+ <script type="text/javascript">
+ (function() {
+ var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
+ script.src = 'https://apis.google.com/js/plusone.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
+ })();
+ </script>
+
<script type="text/javascript">
View
421 2012/12/24/learning-foundation-dot-framework/index.html
@@ -1,421 +0,0 @@
-
-<!DOCTYPE html>
-<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
-<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
-<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
-<head>
- <meta charset="utf-8">
- <title>Learning Foundation.framework - Learning Foundation.framework | Tonny Xu</title>
- <meta name="author" content="Tonny Xu">
-
-
- <meta name="description" content="[Last update: 2012-12-24] Foundation.framework is the most basic framework we use in almost all the iOS/Mac projects. If you can understand this &hellip;">
-
-
- <!-- http://t.co/dKP3o1e -->
- <meta name="HandheldFriendly" content="True">
- <meta name="MobileOptimized" content="320">
- <meta name="viewport" content="width=device-width, initial-scale=1">
-
-
- <link rel="canonical" href="http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework/">
- <link href="/favicon.png" rel="icon">
- <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
- <script src="/javascripts/modernizr-2.0.js"></script>
- <script src="/javascripts/ender.js"></script>
- <script src="/javascripts/octopress.js" type="text/javascript"></script>
- <link href="/atom.xml" rel="alternate" title="Learning Foundation.framework | Tonny Xu" type="application/atom+xml">
- <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
-<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
-<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
-
-
- <script type="text/javascript">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-579328-7']);
- _gaq.push(['_trackPageview']);
-
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>
-
-
-</head>
-
-<body >
- <header role="banner"><hgroup>
- <h1><a href="/">Learning Foundation.framework | Tonny Xu</a></h1>
-
- <h2>Life is like a box of chocolates...</h2>
-
-</hgroup>
-
-</header>
- <nav role="navigation"><ul class="subscription" data-subscription="rss">
- <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
-
-</ul>
-
-<form action="http://google.com/search" method="get">
- <fieldset role="search">
- <input type="hidden" name="q" value="site:TonnyXu.github.com" />
- <input class="search" type="text" name="q" results="0" placeholder="Search"/>
- </fieldset>
-</form>
-
-<ul class="main-navigation">
- <li><a href="/">Blog</a></li>
- <li><a href="/blog/archives">Archives</a></li>
-</ul>
-
-</nav>
- <div id="main">
- <div id="content">
- <div>
-<article class="hentry" role="article">
-
- <header>
-
- <h1 class="entry-title">Learning Foundation.framework</h1>
-
-
- <p class="meta">
-
-
-
-
-
-
-
-
-
-
-
-
-<time datetime="2012-12-24T19:20:00+09:00" pubdate data-updated="true">Dec 24<span>th</span>, 2012</time>
-
- </p>
-
- </header>
-
-
-<div class="entry-content"><p><em>[Last update: 2012-12-24]</em></p>
-
-<p><code>Foundation.framework</code> is the most basic framework we use in almost all the iOS/Mac projects. If you can understand this framework well, it can save you a lot of time. Starting from today, I will try to start a new serial of posts about learning <code>Foundation.framework</code>.</p>
-
-<p><strong>Just bear in mind that as the learning journey goes on, this post will be updated from time to time.</strong></p>
-
-<p>In <a href="#post_organization">the bottom of this post</a>, I organized my posts in different groups, just as you can see in <a href="#class_figure">the figure</a></p>
-
-<h2>Basic Understanding of <code>Foundation.framework</code></h2>
-
-<p>Before we start to study the whole framework, a basic understanding of <code>Foundation.framework</code> is necessary. <code>Foundation.framework</code> is usually referred as <code>Foundation</code>.</p>
-
-<ol>
-<li><code>Foundation</code> is part of Cocoa framework(in OSX), and Cocoa Touch Framework(in iOS). To see what Cocoa is, Apple has a good documentation on <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html#//apple_ref/doc/uid/TP40002974-CH3-SW16">What is Cocoa</a>.</li>
-<li>In OSX, Cocoa includes 2 major frameworks: <code>Foundation</code> and <code>AppKit</code>.</li>
-<li>In iOS, Cocoa Touch includes 2 major frameworks: <code>Foundation</code> and <code>UIKit</code>.</li>
-<li>There are little differences between the <code>Foundation</code> in OSX and iOS, but most of the classes are identical in both platform.</li>
-<li><code>Foundation</code> defines a base layer of classes that can be used for any kind of Cocoa program. Thus <code>Foundation</code> has no relationship to user interface(<code>AppKit</code> or <code>UIKit</code>)</li>
-</ol>
-
-
-<h3>Foundation Design Goal</h3>
-
-<blockquote><ul>
-<li>Define basic object behavior and introduce consistent conventions for such things as memory management, object mutability, and notifications.</li>
-<li>Support internationalization and localization with (among other things) bundle technology and Unicode strings.</li>
-<li>Support object persistence.</li>
-<li>Support object distribution.</li>
-<li>Provide some measure of operating-system independence to support portability.</li>
-<li>Provide object wrappers or equivalents for programmatic primitives, such as numeric values, strings, and collections. It also provides utility classes for accessing underlying system entities and services, such as ports, threads, and file systems.</li>
-</ul>
-
-
-<p>Cocoa applications, which by definition link either against the AppKit framework or UIKit framework, invariably must link against the Foundation framework as well. The class hierarchies share the same root class, NSObject, and many if not most of the AppKit and UIKit methods and functions have Foundation objects as parameters or return values. Some Foundation classes may seem designed for applications—NSUndoManager and NSUserDefaults, to name two—but they are included in Foundation because there can be uses for them that do not involve a user interface.</p></blockquote>
-
-<h3>Foundation Paradigms &amp; Policies</h3>
-
-<blockquote><p>Foundation introduces several paradigms and policies to Cocoa programming to ensure consistent behavior and expectations among the objects of a program in certain situations.:</p>
-
-<ul>
-<li><strong>Object retention and object disposal</strong>. The Objective-C runtime and Foundation give Cocoa programs two ways to ensure that objects persist when they’re needed and are freed when they are no longer needed. <br/>
- <strong>NOTE</strong>: Garbage collection was introduced in Objective-C 2.0 and deprecated since ARC is released. For newly written applications, ARC is recommended. Even the old applications with explicit retain/release paradigm can be upgraded to use ARC.</li>
-<li><strong>Mutable class variants</strong>. Many value and container classes in Foundation have a mutable variant of an immutable class, with the mutable class always being a subclass of the immutable one. If you need to dynamically change the encapsulated value or membership of such an object, you create an instance of the mutable class. Because it inherits from the immutable class, you can pass the mutable instance in methods that take the immutable type. For more on object mutability, see “<a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW33">Object Mutability</a>.”</li>
-<li><strong>Class clusters</strong>. A class cluster is an abstract class and a set of private concrete subclasses for which the abstract class acts as an umbrella interface. Depending on the context (particularly the method you use to create an object), an instance of the appropriate optimized class is returned to you. <code>NSString</code> and <code>NSMutableString</code>, for example, act as brokers for instances of various private subclasses optimized for different kinds of storage needs. Over the years the set of concrete classes has changed several times without breaking applications. For more on class clusters, see “<a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34">Class Clusters</a>.”</li>
-<li><strong>Notifications</strong>. Notification is a major design pattern in Cocoa. It is based on a broadcast mechanism that allows objects (called observers) to be kept informed of what another object is doing or is encountering in the way of user or system events. The object originating the notification can be unaware of the existence or identity of the observers of the notification. There are several types of notifications: synchronous, asynchronous, and distributed. The Foundation notification mechanism is implemented by the <code>NSNotification</code>, <code>NSNotificationCenter</code>, <code>NSNotificationQueue</code>, and <code>NSDistributedNotificationCenter</code> classes. For more on notifications, see “<a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW7">Notifications</a>.”</li>
-</ul>
-</blockquote>
-
-<h2>Know the <code>Foundation</code> Classes</h2>
-
-<p>There are many classes and functions defined in <code>Foundation</code>. Understanding those classes one by one is key to accelerate your development of OSX/iOS apps.</p>
-
-<p>Apple&#8217;s development document already have some very good diagrams, which divided those classes into different groups by functionality.</p>
-
-<h3><a id="class_figure"></a>Figure: The Foundation class hierarchy</h3>
-
-<p><img src="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/objc_foundation_A.jpg" alt="" />
-<img src="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/objc_foundation2_A.jpg" alt="" />
-<img src="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/objc_foundation3_A.jpg" alt="" /></p>
-
-<h3>Read the Release Note of Each Version</h3>
-
-<p>Apple is keeping the document correct and up-to-date, but, to be frank, <em>most of the time</em>, the document can not reflect the most recent changes of each version, so reading the release note for each version is very important.</p>
-
-<p>Here are the related release notes:</p>
-
-<ul>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/Cocoa/FoundationOlder.html#//apple_ref/doc/uid/TP30000742">Foundation Release Note</a></li>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_8.html%23//apple_ref/doc/uid/TP40011634-SW24">OSX 10.8 Release Note:Foundation</a></li>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_7.html%23//apple_ref/doc/uid/TP40010355-SW31">OSX 10.7 Release Note:Foundation</a></li>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_6.html%23//apple_ref/doc/uid/TP40008898-SW32">OSX 10.6 Release Note:Foundation</a></li>
-</ul>
-
-
-<h2><a id="post_organization"></a>The Organization of the Posts</h2>
-
-<p>Since there are so many different classes in the <code>Foundation</code>, it&#8217;s necessary to group my posts as <a href="#class_figure">the figure you see above</a>.</p>
-
-<ol>
-<li>Value Objects</li>
-<li>XML</li>
-<li>Strings</li>
-<li>Collections</li>
-<li>Predicates</li>
-<li>OS Services</li>
-<li>File System</li>
-<li>URL</li>
-<li>Interprocess Communication</li>
-<li>Locking/Threading</li>
-<li>Notifications</li>
-<li>Archiving &amp; Serialization</li>
-<li>ObjC Language Services</li>
-<li>Scripting</li>
-<li>Distributed Objects.</li>
-</ol>
-
-
-<h3>Asking for help</h3>
-
-<p>Since such kind of study is really huge and will cost a long time to archive the goal. I&#8217;m here to ask you to join the journey with me.</p>
-
-<h4>How can you help</h4>
-
-<ol>
-<li>Review the post I write, give me feedback.</li>
-<li>Write posts with me, allow me to post in this site.</li>
-<li>Write sample code with me.</li>
-</ol>
-
-
-<h4>How to Join the Journey</h4>
-
-<ol>
-<li>Twitter: <a href="http://twitter.com/TonnyXu">@TonnyXu</a></li>
-<li>Facebook: <a href="http://facebook.com/Xu.Tonny">Tonny Xu</a></li>
-<li>Email: tonny.xu[AT]gmail.com</li>
-</ol>
-
-
-<hr />
-
-<h2>License - Creative Common 3.0</h2>
-
-<p><a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Loving Cocoa</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://tonnyxu.github.com" property="cc:attributionName" rel="cc:attributionURL">Tonny Xu</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US">Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="http://tonnyxu.github.com" rel="dct:source">http://tonnyxu.github.com</a>.</p>
-</div>
-
-
- <footer>
- <p class="meta">
-
-
-
-<span class="byline author vcard">Posted by <span class="fn">Tonny Xu</span></span>
-
-
-
-
-
-
-
-
-
-
-
-
-
-<time datetime="2012-12-24T19:20:00+09:00" pubdate data-updated="true">Dec 24<span>th</span>, 2012</time>
-
-
-<span class="categories">
-
- <a class='category' href='/blog/categories/cocoa/'>Cocoa</a>, <a class='category' href='/blog/categories/foundation-framework/'>Foundation.framework</a>, <a class='category' href='/blog/categories/objective-c/'>Objective-C</a>
-
-</span>
-
-
- </p>
-
- <div class="sharing">
-
- <a href="http://twitter.com/share" class="twitter-share-button" data-url="http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework/" data-via="TonnyXu" data-counturl="http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework/" >Tweet</a>
-
-
-
- <div class="fb-like" data-send="true" data-width="450" data-show-faces="false"></div>
-
-</div>
-
-
- <p class="meta">
-
- <a class="basic-alignment left" href="/2012/11/21/finished-setting-up-octopress/" title="Previous Post: Finished setting up Octopress">&laquo; Finished setting up Octopress</a>
-
-
- <a class="basic-alignment right" href="/2013/02/10/nsstring-hash-function/" title="Next Post: NSString - Hash function">NSString - Hash function &raquo;</a>
-
- </p>
- </footer>
-</article>
-
- <section>
- <h1>Comments</h1>
- <div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
-</div>
- </section>
-
-</div>
-
-<aside class="sidebar">
-
- <section>
- <h1>Recent Posts</h1>
- <ul id="recent_posts">
-
- <li class="post">
- <a href="/2013/02/10/nsstring-hash-function/">NSString - Hash function</a>
- </li>
-
- <li class="post">
- <a href="/2012/12/24/learning-foundation-dot-framework/">Learning Foundation.framework</a>
- </li>
-
- <li class="post">
- <a href="/2012/11/21/finished-setting-up-octopress/">Finished setting up Octopress</a>
- </li>
-
- </ul>
-</section>
-
-<section>
- <h1>GitHub Repos</h1>
- <ul id="gh_repos">
- <li class="loading">Status updating...</li>
- </ul>
-
- <a href="https://github.com/TonnyXu">@TonnyXu</a> on GitHub
-
- <script type="text/javascript">
- $.domReady(function(){
- if (!window.jXHR){
- var jxhr = document.createElement('script');
- jxhr.type = 'text/javascript';
- jxhr.src = '/javascripts/libs/jXHR.js';
- var s = document.getElementsByTagName('script')[0];
- s.parentNode.insertBefore(jxhr, s);
- }
-
- github.showRepos({
- user: 'TonnyXu',
- count: 5,
- skip_forks: true,
- target: '#gh_repos'
- });
- });
- </script>
- <script src="/javascripts/github.js" type="text/javascript"> </script>
-</section>
-
-
-<section>
- <h1>Latest Tweets</h1>
- <ul id="tweets">
- <li class="loading">Status updating...</li>
- </ul>
- <script type="text/javascript">
- $.domReady(function(){
- getTwitterFeed("TonnyXu", 5, false);
- });
- </script>
- <script src="/javascripts/twitter.js" type="text/javascript"> </script>
-
- <a href="http://twitter.com/TonnyXu" class="twitter-follow-button" data-show-count="false">Follow @TonnyXu</a>
-
-</section>
-
-
-
-</aside>
-
-
- </div>
- </div>
- <footer role="contentinfo"><p>
- Copyright &copy; 2013 - Tonny Xu -
- <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
-</p>
-
-</footer>
-
-
-<script type="text/javascript">
- var disqus_shortname = 'TonnyXu';
-
-
- // var disqus_developer = 1;
- var disqus_identifier = 'http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework/';
- var disqus_url = 'http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework/';
- var disqus_script = 'embed.js';
-
- (function () {
- var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
- dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
- }());
-</script>
-
-
-
-<div id="fb-root"></div>
-<script>(function(d, s, id) {
- var js, fjs = d.getElementsByTagName(s)[0];
- if (d.getElementById(id)) {return;}
- js = d.createElement(s); js.id = id;
- js.src = "//connect.facebook.net/en_US/all.js#appId=212934732101925&xfbml=1";
- fjs.parentNode.insertBefore(js, fjs);
-}(document, 'script', 'facebook-jssdk'));</script>
-
-
-
-
-
- <script type="text/javascript">
- (function(){
- var twitterWidgets = document.createElement('script');
- twitterWidgets.type = 'text/javascript';
- twitterWidgets.async = true;
- twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
- document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
- })();
- </script>
-
-
-
-
-
-</body>
-</html>
View
627 2013/02/10/nsstring-hash-function/index.html
@@ -1,627 +0,0 @@
-
-<!DOCTYPE html>
-<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
-<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
-<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
-<head>
- <meta charset="utf-8">
- <title>NSString - Hash function - Learning Foundation.framework | Tonny Xu</title>
- <meta name="author" content="Tonny Xu">
-
-
- <meta name="description" content="The origin of -[NSString hash] function The -[NSString hash] function is actually defined by NSObject protocol(not the class), it defines -[NSObject &hellip;">
-
-
- <!-- http://t.co/dKP3o1e -->
- <meta name="HandheldFriendly" content="True">
- <meta name="MobileOptimized" content="320">
- <meta name="viewport" content="width=device-width, initial-scale=1">
-
-
- <link rel="canonical" href="http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/">
- <link href="/favicon.png" rel="icon">
- <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
- <script src="/javascripts/modernizr-2.0.js"></script>
- <script src="/javascripts/ender.js"></script>
- <script src="/javascripts/octopress.js" type="text/javascript"></script>
- <link href="/atom.xml" rel="alternate" title="Learning Foundation.framework | Tonny Xu" type="application/atom+xml">
- <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
-<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
-<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
-
-
- <script type="text/javascript">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-579328-7']);
- _gaq.push(['_trackPageview']);
-
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>
-
-
-</head>
-
-<body >
- <header role="banner"><hgroup>
- <h1><a href="/">Learning Foundation.framework | Tonny Xu</a></h1>
-
- <h2>Life is like a box of chocolates...</h2>
-
-</hgroup>
-
-</header>
- <nav role="navigation"><ul class="subscription" data-subscription="rss">
- <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
-
-</ul>
-
-<form action="http://google.com/search" method="get">
- <fieldset role="search">
- <input type="hidden" name="q" value="site:TonnyXu.github.com" />
- <input class="search" type="text" name="q" results="0" placeholder="Search"/>
- </fieldset>
-</form>
-
-<ul class="main-navigation">
- <li><a href="/">Blog</a></li>
- <li><a href="/blog/archives">Archives</a></li>
-</ul>
-
-</nav>
- <div id="main">
- <div id="content">
- <div>
-<article class="hentry" role="article">
-
- <header>
-
- <h1 class="entry-title">NSString - Hash Function</h1>
-
-
- <p class="meta">
-
-
-
-
-
-
-
-
-
-
-
-
-<time datetime="2013-02-10T18:40:00+09:00" pubdate data-updated="true">Feb 10<span>th</span>, 2013</time>
-
- </p>
-
- </header>
-
-
-<div class="entry-content"><h2>The origin of <code>-[NSString hash]</code> function</h2>
-
-<p>The <code>-[NSString hash]</code> function is actually defined by NSObject protocol(not the class), it defines <code>-[NSObject hash]</code> as required. You can see it from <code>NSObject.h</code></p>
-
-<figure class='code'><figcaption><span>the define of `-hash` function</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">@protocol</span> <span class="nc">NSObject</span>
-</span><span class='line'>
-</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">isEqual:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">object</span><span class="p">;</span>
-</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="n">NSUInteger</span><span class="p">)</span><span class="nf">hash</span><span class="p">;</span>
-</span><span class='line'>
-</span><span class='line'><span class="c1">// others ignored.</span>
-</span><span class='line'><span class="k">@end</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>And since NSObject class adopts <code>NSObject</code> protocol, hence every object definitely has a function called <code>-hash</code>. While different classes have different types of data, writing a universal hash function for all the classes is not possible, so the classes in <code>Foundation.framework</code> implemented different hash function by override NSObject class&#8217;s implementation.</p>
-
-<p><a id="note64"></a>
-One thing to be <strong>NOTED</strong>: the return type of <code>-[NSObject hash]</code> function is <code>NSUInteger</code>. Because <code>NSUInteger</code> has different sizes on x86 and x86_64 and ARM systems, when implement your own class&#8217;s hash function, don&#8217;t forget that.</p>
-
-<h2>When this function will be used?</h2>
-
-<p>When we write our code, we rarely use hash function directly. Why? Because it is usually used by container class <code>NSDictionary</code> which is wildly called <strong>hash table</strong>.</p>
-
-<blockquote><p>If you want to have a deep understanding of <strong>hash table</strong>, please see the <a href="http://en.wikipedia.org/wiki/Hash_table">wikipedia page</a>.</p></blockquote>
-
-<p>In short, hash table is a data structure that support O(1) <strong>data access</strong> by searching the <strong>key</strong>. Do reach that goal:</p>
-
-<ol>
-<li>The keys a hash table contains must be <strong>unique</strong>.</li>
-<li>A particular key can always be <strong>hashed</strong> to the same value.</li>
-</ol>
-
-
-<p>One basic idea is that we can generate a integer hash value for each key so that each value is <strong>unique</strong>, and use this unique key to insert/delete/search the value. If the key is a decimal, that it would be easier, but numbers don&#8217;t have meanings. Using a string to represent a key will make our code(and our life) much more easier.</p>
-
-<p>As a result, modern programing languages implemented hash table all support using a string as a key. That&#8217;s when the <code>NSString</code>&#8217;s hash function is used.</p>
-
-<h2>How <code>NSString</code> implements the hash function?</h2>
-
-<p>We talked about the key must be an <strong>unique decimal number</strong>. So the problem will be <strong>how to turn a string into a unique number without collisions</strong>. Actually this doesn&#8217;t turn out to be an easy task. Many computer scientists had researched this topic and see <a href="http://www.amazon.com/gp/product/0262533057/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0262533057&amp;linkCode=as2&amp;tag=totonet-20">Introduction to algorithms, chapter 11</a>(a.k.a. CLRS) for details.</p>
-
-<p>To understand how <code>NSString</code> implements the hash function, the best way is reading the source code, and Apple released the source code of <code>CoreFunction</code> which includes <code>CFString</code>, a toll-free bridge object to <code>NSString</code>, we can see the <a href="http://opensource.apple.com/source/CF/CF-744.12/CFString.c">source code of CFString</a> now.</p>
-
-<h3>Step 1: Grab the source code</h3>
-
-<p>To grab the source code, just access <a href="http://opensource.apple.com/">http://opensource.apple.com/</a>, navigate to a particular Mac OS X release, search for <strong>CF-</strong>, then you will find a link on the right to download the source code.</p>
-
-<h3>Step 2: Open the source tree and navigate to <code>CFString.c</code></h3>
-
-<p>Using vim/emacs or any text editor you like. There are 4 <em>public</em> functions defined:</p>
-
-<figure class='code'><figcaption><span>4 hash functions defined in CFString</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="n">CFStringHashCString</span><span class="p">(</span><span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span><span class="n">bytes</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">)</span>
-</span><span class='line'><span class="n">CFStringHashCharacters</span><span class="p">(</span><span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">characters</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">)</span>
-</span><span class='line'><span class="n">CFStringHashISOLatin1CString</span><span class="p">(</span><span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span><span class="n">bytes</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">)</span>
-</span><span class='line'><span class="n">CFStringHashNSString</span><span class="p">(</span><span class="n">CFStringRef</span> <span class="n">str</span><span class="p">)</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Here is the start point of our journey.</p>
-
-<h3>Step 3: Let the journey begin</h3>
-
-<p>Let&#8217;s take a deep look into the <code>CFStringHashNSString(CFStringRef str)</code> because from what the function name tells, it&#8217;s the function we are looking for.</p>
-
-<p><a id="list1"></a></p>
-
-<figure class='code'><figcaption><span>List 1</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-<span class='line-number'>8</span>
-<span class='line-number'>9</span>
-<span class='line-number'>10</span>
-<span class='line-number'>11</span>
-<span class='line-number'>12</span>
-<span class='line-number'>13</span>
-<span class='line-number'>14</span>
-<span class='line-number'>15</span>
-<span class='line-number'>16</span>
-<span class='line-number'>17</span>
-<span class='line-number'>18</span>
-<span class='line-number'>19</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cm">/* This is meant to be called from NSString or subclassers only. It is an error for this to be called without the ObjC runtime or an argument which is not an NSString or subclass. It can be called with NSCFString, although that would be inefficient (causing indirection) and won&#39;t normally happen anyway, as NSCFString overrides hash.</span>
-</span><span class='line'><span class="cm">*/</span>
-</span><span class='line'><span class="n">CFHashCode</span> <span class="nf">CFStringHashNSString</span><span class="p">(</span><span class="n">CFStringRef</span> <span class="n">str</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="n">UniChar</span> <span class="n">buffer</span><span class="p">[</span><span class="n">HashEverythingLimit</span><span class="p">];</span>
-</span><span class='line'> <span class="n">CFIndex</span> <span class="n">bufLen</span><span class="p">;</span> <span class="c1">// Number of characters in the buffer for hashing</span>
-</span><span class='line'> <span class="n">CFIndex</span> <span class="n">len</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// Actual length of the string</span>
-</span><span class='line'>
-</span><span class='line'> <span class="n">len</span> <span class="o">=</span> <span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">length</span><span class="p">);</span>
-</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">len</span> <span class="o">&lt;=</span> <span class="n">HashEverythingLimit</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">len</span><span class="p">));</span>
-</span><span class='line'> <span class="n">bufLen</span> <span class="o">=</span> <span class="n">len</span><span class="p">;</span>
-</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">32</span><span class="p">));</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span><span class="o">+</span><span class="mi">32</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">((</span><span class="n">len</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">));</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span><span class="o">+</span><span class="mi">64</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">(</span><span class="n">len</span> <span class="o">-</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">32</span><span class="p">));</span>
-</span><span class='line'> <span class="n">bufLen</span> <span class="o">=</span> <span class="n">HashEverythingLimit</span><span class="p">;</span>
-</span><span class='line'> <span class="p">}</span>
-</span><span class='line'> <span class="k">return</span> <span class="n">__CFStrHashCharacters</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">bufLen</span><span class="p">,</span> <span class="n">len</span><span class="p">);</span>
-</span><span class='line'><span class="p">}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Note the comment at the beginning of this function. Because <code>NSCFString</code> is completely private, we don&#8217;t have a chance to see how <code>NSCFString</code> to implement this. But, this function also clearly demonstrates how to implement a hash function, so let&#8217;s continue our journey with this function.</p>
-
-<h4>The define of <code>HashEverythingLimit</code></h4>
-
-<p>Basically, the <code>if</code> clause divides this function into 2 parts. The length of the string is more than <code>HashEverythingLimit</code> or not.
-<code>HashEverythingLimit</code> is defined as:</p>
-
-<p><a id="list2"></a></p>
-
-<figure class='code'><figcaption><span>List 2</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-<span class='line-number'>8</span>
-<span class='line-number'>9</span>
-<span class='line-number'>10</span>
-<span class='line-number'>11</span>
-<span class='line-number'>12</span>
-<span class='line-number'>13</span>
-<span class='line-number'>14</span>
-<span class='line-number'>15</span>
-<span class='line-number'>16</span>
-<span class='line-number'>17</span>
-<span class='line-number'>18</span>
-<span class='line-number'>19</span>
-<span class='line-number'>20</span>
-<span class='line-number'>21</span>
-<span class='line-number'>22</span>
-<span class='line-number'>23</span>
-<span class='line-number'>24</span>
-<span class='line-number'>25</span>
-<span class='line-number'>26</span>
-<span class='line-number'>27</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cm">/* String hashing: Should give the same results whatever the encoding; so we hash UniChars.</span>
-</span><span class='line'><span class="cm">If the length is less than or equal to 96, then the hash function is simply the </span>
-</span><span class='line'><span class="cm">following (n is the nth UniChar character, starting from 0):</span>
-</span><span class='line'><span class="cm"> </span>
-</span><span class='line'><span class="cm"> hash(-1) = length</span>
-</span><span class='line'><span class="cm"> hash(n) = hash(n-1) * 257 + unichar(n);</span>
-</span><span class='line'><span class="cm"> Hash = hash(length-1) * ((length &amp; 31) + 1)</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">If the length is greater than 96, then the above algorithm applies to </span>
-</span><span class='line'><span class="cm">characters 0..31, (length/2)-16..(length/2)+15, and length-32..length-1, inclusive;</span>
-</span><span class='line'><span class="cm">thus the first, middle, and last 32 characters.</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">Note that the loops below are unrolled; and: 257^2 = 66049; 257^3 = 16974593; 257^4 = 4362470401; 67503105 is 257^4 - 256^4</span>
-</span><span class='line'><span class="cm">If hashcode is changed from UInt32 to something else, this last piece needs to be readjusted. </span>
-</span><span class='line'><span class="cm">!!! We haven&#39;t updated for LP64 yet</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">NOTE: The hash algorithm used to be duplicated in CF and Foundation; but now it should only be in the four functions below.</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">Hash function was changed between Panther and Tiger, and Tiger and Leopard.</span>
-</span><span class='line'><span class="cm">*/</span>
-</span><span class='line'><span class="cp">#define HashEverythingLimit 96</span>
-</span><span class='line'>
-</span><span class='line'><span class="cp">#define HashNextFourUniChars(accessStart, accessEnd, pointer) \</span>
-</span><span class='line'><span class="cp"> {result = result * 67503105 + (accessStart 0 accessEnd) * 16974593 + (accessStart 1 accessEnd) * 66049 + (accessStart 2 accessEnd) * 257 + (accessStart 3 accessEnd); pointer += 4;}</span>
-</span><span class='line'>
-</span><span class='line'><span class="cp">#define HashNextUniChar(accessStart, accessEnd, pointer) \</span>
-</span><span class='line'><span class="cp"> {result = result * 257 + (accessStart 0 accessEnd); pointer++;}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>The reason I post other context is because the comment before <code>HashEverythingLimit</code> well explained how apple&#8217;s guys were thinking.</p>
-
-<ol>
-<li>If the string is less than 96 characters, hash it directly.</li>
-<li>If the string has more than 96 characters, get the first, middle, last 32 characters to form a 96 characters string, then hash the 96 characters string.</li>
-</ol>
-
-
-<p>You can see the code in the <a href="#gist">gist</a> below.</p>
-
-<p>This explains what the <code>if</code> clause is doing in <a href="#list1">List 1</a>.</p>
-
-<h4>This inline function does the real work</h4>
-
-<p>Back to <a href="#list1">List 1</a>, we can see the <code>if</code> clause is actually forming a temp string buffer to be used to calculate the hash value. The real work is done in <code>return</code> line.</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">return</span> <span class="nf">__CFStrHashCharacters</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">bufLen</span><span class="p">,</span> <span class="n">len</span><span class="p">);</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>OK, let&#8217;s see what this line function <code>__CFStrHashCharacters</code> really does.</p>
-
-<p><a id="list3"></a></p>
-
-<figure class='code'><figcaption><span>List 3: __CFStrHashCharacters</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-<span class='line-number'>8</span>
-<span class='line-number'>9</span>
-<span class='line-number'>10</span>
-<span class='line-number'>11</span>
-<span class='line-number'>12</span>
-<span class='line-number'>13</span>
-<span class='line-number'>14</span>
-<span class='line-number'>15</span>
-<span class='line-number'>16</span>
-<span class='line-number'>17</span>
-<span class='line-number'>18</span>
-<span class='line-number'>19</span>
-<span class='line-number'>20</span>
-<span class='line-number'>21</span>
-<span class='line-number'>22</span>
-<span class='line-number'>23</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cm">/* In this function, actualLen is the length of the original string; but len is the number of characters in buffer. The buffer is expected to contain the parts of the string relevant to hashing.</span>
-</span><span class='line'><span class="cm">*/</span>
-</span><span class='line'><span class="n">CF_INLINE</span> <span class="n">CFHashCode</span> <span class="nf">__CFStrHashCharacters</span><span class="p">(</span><span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">uContents</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">actualLen</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="n">CFHashCode</span> <span class="n">result</span> <span class="o">=</span> <span class="n">actualLen</span><span class="p">;</span>
-</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">len</span> <span class="o">&lt;=</span> <span class="n">HashEverythingLimit</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">end4</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="p">(</span><span class="n">len</span> <span class="o">&amp;</span> <span class="o">~</span><span class="mi">3</span><span class="p">);</span>
-</span><span class='line'> <span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">end</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="n">len</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">uContents</span> <span class="o">&lt;</span> <span class="n">end4</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">uContents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">uContents</span><span class="p">);</span> <span class="c1">// First count in fours</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">uContents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextUniChar</span><span class="p">(</span><span class="n">uContents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">uContents</span><span class="p">);</span> <span class="c1">// Then for the last &lt;4 chars, count in ones...</span>
-</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
-</span><span class='line'> <span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">contents</span><span class="p">,</span> <span class="o">*</span><span class="n">end</span><span class="p">;</span>
-</span><span class='line'> <span class="n">contents</span> <span class="o">=</span> <span class="n">uContents</span><span class="p">;</span>
-</span><span class='line'> <span class="n">end</span> <span class="o">=</span> <span class="n">contents</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">contents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">contents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">contents</span><span class="p">);</span>
-</span><span class='line'> <span class="n">contents</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="p">(</span><span class="n">len</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">16</span><span class="p">;</span>
-</span><span class='line'> <span class="n">end</span> <span class="o">=</span> <span class="n">contents</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">contents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">contents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">contents</span><span class="p">);</span>
-</span><span class='line'> <span class="n">end</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="n">len</span><span class="p">;</span>
-</span><span class='line'> <span class="n">contents</span> <span class="o">=</span> <span class="n">end</span> <span class="o">-</span> <span class="mi">32</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">contents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">contents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">contents</span><span class="p">);</span>
-</span><span class='line'> <span class="p">}</span>
-</span><span class='line'> <span class="k">return</span> <span class="n">result</span> <span class="o">+</span> <span class="p">(</span><span class="n">result</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">actualLen</span> <span class="o">&amp;</span> <span class="mi">31</span><span class="p">));</span>
-</span><span class='line'><span class="p">}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Let&#8217;s take a close look at this method, the whole thing before <code>return</code> is to calculate a fairly large number <code>result</code>, than add another large value by left shifting <code>result</code>.</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">return</span> <span class="n">result</span> <span class="o">+</span> <span class="p">(</span><span class="n">result</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">actualLen</span> <span class="o">&amp;</span> <span class="mi">31</span><span class="p">));</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Note there are some programming technicals used here, especially an unusual usage of macro:</p>
-
-<p>With the definition of <code>HashNextUniChar</code> as</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#define HashNextUniChar(accessStart, accessEnd, pointer) \</span>
-</span><span class='line'><span class="cp"> {result = result * 257 + (accessStart 0 accessEnd); pointer++;}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>The code below:</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="n">HashNextUniChar</span><span class="p">(</span><span class="n">uContents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">uContents</span><span class="p">);</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>will be expanded to:</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="p">{</span><span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="mi">257</span> <span class="o">+</span> <span class="n">uContents</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="n">uContents</span><span class="o">++</span><span class="p">;}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p><strong>That&#8217;s it</strong>.</p>
-
-<h2>Verify the hash function</h2>
-
-<p>So far, we understand how the hash value of a NSString is generated. Since the root function is <code>CFStringHashNSString</code> we can place a symbolic breakpoint on this function and <code>__CFStringHash</code> in Xcode and see what will happen. Note that the core function to calculate the hash value <code>__CFStrHashCharacters</code> in <a href="#list3">List 3</a> is marked <code>CF_INLINE</code> we could not put a breakpoint on this function.</p>
-
-<p><img src="/images/Screen%20Shot%202013-02-10%20at%208.28.54%20PM.png" alt="image of putting a symbolic breakpoint" /></p>
-
-<p>I wrote a simple function to see the differences of 2 functions: <code>-[NSString hash]</code> and <code>CFStringHashNSString</code>.</p>
-
-<p><a id="gist"></a></p>
-
-<div><script src='https://gist.github.com/4749305.js'></script>
-<noscript><pre><code></code></pre></noscript></div>
-
-
-
-
-<div><script src='https://gist.github.com/4749305.js?file=[output.txt]'></script>
-<noscript><pre><code>2013-02-10 21:12:57.704 NSStringHash[25996:303] 3294171457232409974
-2013-02-10 21:12:57.707 NSStringHash[25996:303] -1315859082
-2013-02-10 21:12:57.707 NSStringHash[25996:303] -1315859082
-2013-02-10 21:12:57.707 NSStringHash[25996:303] 2DB73F7CB1919576
-2013-02-10 21:12:57.708 NSStringHash[25996:303] FFFFFFFFB1919576
-2013-02-10 21:12:57.708 NSStringHash[25996:303] FFFFFFFFB1919576
-2013-02-10 21:12:57.708 NSStringHash[25996:303] -8069816457736624960
-2013-02-10 21:12:57.709 NSStringHash[25996:303] -8069816457736624960
-2013-02-10 21:12:57.709 NSStringHash[25996:303] 900240AFFA01E4C0
-2013-02-10 21:12:57.710 NSStringHash[25996:303] 900240AFFA01E4C0</code></pre></noscript></div>
-
-
-<p>If see the output, did you remember I mentioned to differences between 32bit system and 64bit system <a href="#note64">before</a>?</p>
-
-<p>The hash value returned by <code>CFStringHashNSString</code> is actually a 32bit unsigned integer, while <code>-[NSString hash]</code> does return a 64bit unsigned integer in 64bit system. If we look at <a href="#list2">List 2</a> again, we will see that Apple&#8217;s engineer noted that they haven&#8217;t implemented <code>LP64</code> yet.</p>
-
-<p>And the last demo code shows if you calculate the hash value for 2 strings longer than 96 characters but have the same characters in first, middle, last 32 characters, we did get 2 hash codes <strong>exactly the same</strong>.</p>
-
-<h2>Problems left</h2>
-
-<p>But if we put a breakpoint on <code>__CFStringHash</code> function, we will see it does get called when <code>-[NSString hash]</code> is called. And if we call <code>__CFStringHash</code> directly we got the same result as <code>CFStringHashNSString</code>. Looks like the way we pass in the parameter to <code>__CFStringHash</code> is wrong.</p>
-
-<p>Now I&#8217;m not so clear about how get a 64bit long hash value when we call <code>__CFStringHash</code> or <code>CFStringHashNSString</code> directly. If anyone knows, please leave a message.</p>
-</div>
-
-
- <footer>
- <p class="meta">
-
-
-
-<span class="byline author vcard">Posted by <span class="fn">Tonny Xu</span></span>
-
-
-
-
-
-
-
-
-
-
-
-
-
-<time datetime="2013-02-10T18:40:00+09:00" pubdate data-updated="true">Feb 10<span>th</span>, 2013</time>
-
-
-<span class="categories">
-
- <a class='category' href='/blog/categories/nsstring/'>NSString</a>, <a class='category' href='/blog/categories/algorithm/'>algorithm</a>, <a class='category' href='/blog/categories/hash/'>hash</a>
-
-</span>
-
-
- </p>
-
- <div class="sharing">
-
- <a href="http://twitter.com/share" class="twitter-share-button" data-url="http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/" data-via="TonnyXu" data-counturl="http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/" >Tweet</a>
-
-
-
- <div class="fb-like" data-send="true" data-width="450" data-show-faces="false"></div>
-
-</div>
-
-
- <p class="meta">
-
- <a class="basic-alignment left" href="/2012/12/24/learning-foundation-dot-framework/" title="Previous Post: Learning Foundation.framework">&laquo; Learning Foundation.framework</a>
-
-
- </p>
- </footer>
-</article>
-
- <section>
- <h1>Comments</h1>
- <div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
-</div>
- </section>
-
-</div>
-
-<aside class="sidebar">
-
- <section>
- <h1>Recent Posts</h1>
- <ul id="recent_posts">
-
- <li class="post">
- <a href="/2013/02/10/nsstring-hash-function/">NSString - Hash function</a>
- </li>
-
- <li class="post">
- <a href="/2012/12/24/learning-foundation-dot-framework/">Learning Foundation.framework</a>
- </li>
-
- <li class="post">
- <a href="/2012/11/21/finished-setting-up-octopress/">Finished setting up Octopress</a>
- </li>
-
- </ul>
-</section>
-
-<section>
- <h1>GitHub Repos</h1>
- <ul id="gh_repos">
- <li class="loading">Status updating...</li>
- </ul>
-
- <a href="https://github.com/TonnyXu">@TonnyXu</a> on GitHub
-
- <script type="text/javascript">
- $.domReady(function(){
- if (!window.jXHR){
- var jxhr = document.createElement('script');
- jxhr.type = 'text/javascript';
- jxhr.src = '/javascripts/libs/jXHR.js';
- var s = document.getElementsByTagName('script')[0];
- s.parentNode.insertBefore(jxhr, s);
- }
-
- github.showRepos({
- user: 'TonnyXu',
- count: 5,
- skip_forks: true,
- target: '#gh_repos'
- });
- });
- </script>
- <script src="/javascripts/github.js" type="text/javascript"> </script>
-</section>
-
-
-<section>
- <h1>Latest Tweets</h1>
- <ul id="tweets">
- <li class="loading">Status updating...</li>
- </ul>
- <script type="text/javascript">
- $.domReady(function(){
- getTwitterFeed("TonnyXu", 5, false);
- });
- </script>
- <script src="/javascripts/twitter.js" type="text/javascript"> </script>
-
- <a href="http://twitter.com/TonnyXu" class="twitter-follow-button" data-show-count="false">Follow @TonnyXu</a>
-
-</section>
-
-
-
-</aside>
-
-
- </div>
- </div>
- <footer role="contentinfo"><p>
- Copyright &copy; 2013 - Tonny Xu -
- <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
-</p>
-
-</footer>
-
-
-<script type="text/javascript">
- var disqus_shortname = 'TonnyXu';
-
-
- // var disqus_developer = 1;
- var disqus_identifier = 'http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/';
- var disqus_url = 'http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/';
- var disqus_script = 'embed.js';
-
- (function () {
- var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
- dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
- }());
-</script>
-
-
-
-<div id="fb-root"></div>
-<script>(function(d, s, id) {
- var js, fjs = d.getElementsByTagName(s)[0];
- if (d.getElementById(id)) {return;}
- js = d.createElement(s); js.id = id;
- js.src = "//connect.facebook.net/en_US/all.js#appId=212934732101925&xfbml=1";
- fjs.parentNode.insertBefore(js, fjs);
-}(document, 'script', 'facebook-jssdk'));</script>
-
-
-
-
-
- <script type="text/javascript">
- (function(){
- var twitterWidgets = document.createElement('script');
- twitterWidgets.type = 'text/javascript';
- twitterWidgets.async = true;
- twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
- document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
- })();
- </script>
-
-
-
-
-
-</body>
-</html>
View
480 atom.xml
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
- <title><![CDATA[Learning Foundation.framework | Tonny Xu]]></title>
+ <title><![CDATA[Tonny Xu]]></title>
<link href="http://TonnyXu.github.com/atom.xml" rel="self"/>
<link href="http://TonnyXu.github.com/"/>
- <updated>2013-02-11T02:00:23+09:00</updated>
+ <updated>2013-11-30T18:39:22+09:00</updated>
<id>http://TonnyXu.github.com/</id>
<author>
<name><![CDATA[Tonny Xu]]></name>
@@ -14,478 +14,6 @@
<entry>
- <title type="html"><![CDATA[NSString - Hash function]]></title>
- <link href="http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/"/>
- <updated>2013-02-10T18:40:00+09:00</updated>
- <id>http://TonnyXu.github.com/2013/02/10/nsstring-hash-function</id>
- <content type="html"><![CDATA[<h2>The origin of <code>-[NSString hash]</code> function</h2>
-
-<p>The <code>-[NSString hash]</code> function is actually defined by NSObject protocol(not the class), it defines <code>-[NSObject hash]</code> as required. You can see it from <code>NSObject.h</code></p>
-
-<figure class='code'><figcaption><span>the define of `-hash` function</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">@protocol</span> <span class="nc">NSObject</span>
-</span><span class='line'>
-</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">isEqual:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">object</span><span class="p">;</span>
-</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="n">NSUInteger</span><span class="p">)</span><span class="nf">hash</span><span class="p">;</span>
-</span><span class='line'>
-</span><span class='line'><span class="c1">// others ignored.</span>
-</span><span class='line'><span class="k">@end</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>And since NSObject class adopts <code>NSObject</code> protocol, hence every object definitely has a function called <code>-hash</code>. While different classes have different types of data, writing a universal hash function for all the classes is not possible, so the classes in <code>Foundation.framework</code> implemented different hash function by override NSObject class&#8217;s implementation.</p>
-
-<p><a id="note64"></a>
-One thing to be <strong>NOTED</strong>: the return type of <code>-[NSObject hash]</code> function is <code>NSUInteger</code>. Because <code>NSUInteger</code> has different sizes on x86 and x86_64 and ARM systems, when implement your own class&#8217;s hash function, don&#8217;t forget that.</p>
-
-<h2>When this function will be used?</h2>
-
-<p>When we write our code, we rarely use hash function directly. Why? Because it is usually used by container class <code>NSDictionary</code> which is wildly called <strong>hash table</strong>.</p>
-
-<blockquote><p>If you want to have a deep understanding of <strong>hash table</strong>, please see the <a href="http://en.wikipedia.org/wiki/Hash_table">wikipedia page</a>.</p></blockquote>
-
-<p>In short, hash table is a data structure that support O(1) <strong>data access</strong> by searching the <strong>key</strong>. Do reach that goal:</p>
-
-<ol>
-<li>The keys a hash table contains must be <strong>unique</strong>.</li>
-<li>A particular key can always be <strong>hashed</strong> to the same value.</li>
-</ol>
-
-
-<p>One basic idea is that we can generate a integer hash value for each key so that each value is <strong>unique</strong>, and use this unique key to insert/delete/search the value. If the key is a decimal, that it would be easier, but numbers don&#8217;t have meanings. Using a string to represent a key will make our code(and our life) much more easier.</p>
-
-<p>As a result, modern programing languages implemented hash table all support using a string as a key. That&#8217;s when the <code>NSString</code>&#8217;s hash function is used.</p>
-
-<h2>How <code>NSString</code> implements the hash function?</h2>
-
-<p>We talked about the key must be an <strong>unique decimal number</strong>. So the problem will be <strong>how to turn a string into a unique number without collisions</strong>. Actually this doesn&#8217;t turn out to be an easy task. Many computer scientists had researched this topic and see <a href="http://www.amazon.com/gp/product/0262533057/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0262533057&amp;linkCode=as2&amp;tag=totonet-20">Introduction to algorithms, chapter 11</a>(a.k.a. CLRS) for details.</p>
-
-<p>To understand how <code>NSString</code> implements the hash function, the best way is reading the source code, and Apple released the source code of <code>CoreFunction</code> which includes <code>CFString</code>, a toll-free bridge object to <code>NSString</code>, we can see the <a href="http://opensource.apple.com/source/CF/CF-744.12/CFString.c">source code of CFString</a> now.</p>
-
-<h3>Step 1: Grab the source code</h3>
-
-<p>To grab the source code, just access <a href="http://opensource.apple.com/">http://opensource.apple.com/</a>, navigate to a particular Mac OS X release, search for <strong>CF-</strong>, then you will find a link on the right to download the source code.</p>
-
-<h3>Step 2: Open the source tree and navigate to <code>CFString.c</code></h3>
-
-<p>Using vim/emacs or any text editor you like. There are 4 <em>public</em> functions defined:</p>
-
-<figure class='code'><figcaption><span>4 hash functions defined in CFString</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="n">CFStringHashCString</span><span class="p">(</span><span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span><span class="n">bytes</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">)</span>
-</span><span class='line'><span class="n">CFStringHashCharacters</span><span class="p">(</span><span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">characters</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">)</span>
-</span><span class='line'><span class="n">CFStringHashISOLatin1CString</span><span class="p">(</span><span class="k">const</span> <span class="kt">uint8_t</span> <span class="o">*</span><span class="n">bytes</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">)</span>
-</span><span class='line'><span class="n">CFStringHashNSString</span><span class="p">(</span><span class="n">CFStringRef</span> <span class="n">str</span><span class="p">)</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Here is the start point of our journey.</p>
-
-<h3>Step 3: Let the journey begin</h3>
-
-<p>Let&#8217;s take a deep look into the <code>CFStringHashNSString(CFStringRef str)</code> because from what the function name tells, it&#8217;s the function we are looking for.</p>
-
-<p><a id="list1"></a></p>
-
-<figure class='code'><figcaption><span>List 1</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-<span class='line-number'>8</span>
-<span class='line-number'>9</span>
-<span class='line-number'>10</span>
-<span class='line-number'>11</span>
-<span class='line-number'>12</span>
-<span class='line-number'>13</span>
-<span class='line-number'>14</span>
-<span class='line-number'>15</span>
-<span class='line-number'>16</span>
-<span class='line-number'>17</span>
-<span class='line-number'>18</span>
-<span class='line-number'>19</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cm">/* This is meant to be called from NSString or subclassers only. It is an error for this to be called without the ObjC runtime or an argument which is not an NSString or subclass. It can be called with NSCFString, although that would be inefficient (causing indirection) and won&#39;t normally happen anyway, as NSCFString overrides hash.</span>
-</span><span class='line'><span class="cm">*/</span>
-</span><span class='line'><span class="n">CFHashCode</span> <span class="nf">CFStringHashNSString</span><span class="p">(</span><span class="n">CFStringRef</span> <span class="n">str</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="n">UniChar</span> <span class="n">buffer</span><span class="p">[</span><span class="n">HashEverythingLimit</span><span class="p">];</span>
-</span><span class='line'> <span class="n">CFIndex</span> <span class="n">bufLen</span><span class="p">;</span> <span class="c1">// Number of characters in the buffer for hashing</span>
-</span><span class='line'> <span class="n">CFIndex</span> <span class="n">len</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// Actual length of the string</span>
-</span><span class='line'>
-</span><span class='line'> <span class="n">len</span> <span class="o">=</span> <span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">length</span><span class="p">);</span>
-</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">len</span> <span class="o">&lt;=</span> <span class="n">HashEverythingLimit</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">len</span><span class="p">));</span>
-</span><span class='line'> <span class="n">bufLen</span> <span class="o">=</span> <span class="n">len</span><span class="p">;</span>
-</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">32</span><span class="p">));</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span><span class="o">+</span><span class="mi">32</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">((</span><span class="n">len</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">));</span>
-</span><span class='line'> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">CF_OBJC_CALLV</span><span class="p">((</span><span class="n">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">str</span><span class="p">,</span> <span class="n">getCharacters</span><span class="o">:</span><span class="n">buffer</span><span class="o">+</span><span class="mi">64</span> <span class="n">range</span><span class="o">:</span><span class="n">NSMakeRange</span><span class="p">(</span><span class="n">len</span> <span class="o">-</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">32</span><span class="p">));</span>
-</span><span class='line'> <span class="n">bufLen</span> <span class="o">=</span> <span class="n">HashEverythingLimit</span><span class="p">;</span>
-</span><span class='line'> <span class="p">}</span>
-</span><span class='line'> <span class="k">return</span> <span class="n">__CFStrHashCharacters</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">bufLen</span><span class="p">,</span> <span class="n">len</span><span class="p">);</span>
-</span><span class='line'><span class="p">}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Note the comment at the beginning of this function. Because <code>NSCFString</code> is completely private, we don&#8217;t have a chance to see how <code>NSCFString</code> to implement this. But, this function also clearly demonstrates how to implement a hash function, so let&#8217;s continue our journey with this function.</p>
-
-<h4>The define of <code>HashEverythingLimit</code></h4>
-
-<p>Basically, the <code>if</code> clause divides this function into 2 parts. The length of the string is more than <code>HashEverythingLimit</code> or not.
-<code>HashEverythingLimit</code> is defined as:</p>
-
-<p><a id="list2"></a></p>
-
-<figure class='code'><figcaption><span>List 2</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-<span class='line-number'>8</span>
-<span class='line-number'>9</span>
-<span class='line-number'>10</span>
-<span class='line-number'>11</span>
-<span class='line-number'>12</span>
-<span class='line-number'>13</span>
-<span class='line-number'>14</span>
-<span class='line-number'>15</span>
-<span class='line-number'>16</span>
-<span class='line-number'>17</span>
-<span class='line-number'>18</span>
-<span class='line-number'>19</span>
-<span class='line-number'>20</span>
-<span class='line-number'>21</span>
-<span class='line-number'>22</span>
-<span class='line-number'>23</span>
-<span class='line-number'>24</span>
-<span class='line-number'>25</span>
-<span class='line-number'>26</span>
-<span class='line-number'>27</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cm">/* String hashing: Should give the same results whatever the encoding; so we hash UniChars.</span>
-</span><span class='line'><span class="cm">If the length is less than or equal to 96, then the hash function is simply the </span>
-</span><span class='line'><span class="cm">following (n is the nth UniChar character, starting from 0):</span>
-</span><span class='line'><span class="cm"> </span>
-</span><span class='line'><span class="cm"> hash(-1) = length</span>
-</span><span class='line'><span class="cm"> hash(n) = hash(n-1) * 257 + unichar(n);</span>
-</span><span class='line'><span class="cm"> Hash = hash(length-1) * ((length &amp; 31) + 1)</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">If the length is greater than 96, then the above algorithm applies to </span>
-</span><span class='line'><span class="cm">characters 0..31, (length/2)-16..(length/2)+15, and length-32..length-1, inclusive;</span>
-</span><span class='line'><span class="cm">thus the first, middle, and last 32 characters.</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">Note that the loops below are unrolled; and: 257^2 = 66049; 257^3 = 16974593; 257^4 = 4362470401; 67503105 is 257^4 - 256^4</span>
-</span><span class='line'><span class="cm">If hashcode is changed from UInt32 to something else, this last piece needs to be readjusted. </span>
-</span><span class='line'><span class="cm">!!! We haven&#39;t updated for LP64 yet</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">NOTE: The hash algorithm used to be duplicated in CF and Foundation; but now it should only be in the four functions below.</span>
-</span><span class='line'>
-</span><span class='line'><span class="cm">Hash function was changed between Panther and Tiger, and Tiger and Leopard.</span>
-</span><span class='line'><span class="cm">*/</span>
-</span><span class='line'><span class="cp">#define HashEverythingLimit 96</span>
-</span><span class='line'>
-</span><span class='line'><span class="cp">#define HashNextFourUniChars(accessStart, accessEnd, pointer) \</span>
-</span><span class='line'><span class="cp"> {result = result * 67503105 + (accessStart 0 accessEnd) * 16974593 + (accessStart 1 accessEnd) * 66049 + (accessStart 2 accessEnd) * 257 + (accessStart 3 accessEnd); pointer += 4;}</span>
-</span><span class='line'>
-</span><span class='line'><span class="cp">#define HashNextUniChar(accessStart, accessEnd, pointer) \</span>
-</span><span class='line'><span class="cp"> {result = result * 257 + (accessStart 0 accessEnd); pointer++;}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>The reason I post other context is because the comment before <code>HashEverythingLimit</code> well explained how apple&#8217;s guys were thinking.</p>
-
-<ol>
-<li>If the string is less than 96 characters, hash it directly.</li>
-<li>If the string has more than 96 characters, get the first, middle, last 32 characters to form a 96 characters string, then hash the 96 characters string.</li>
-</ol>
-
-
-<p>You can see the code in the <a href="#gist">gist</a> below.</p>
-
-<p>This explains what the <code>if</code> clause is doing in <a href="#list1">List 1</a>.</p>
-
-<h4>This inline function does the real work</h4>
-
-<p>Back to <a href="#list1">List 1</a>, we can see the <code>if</code> clause is actually forming a temp string buffer to be used to calculate the hash value. The real work is done in <code>return</code> line.</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">return</span> <span class="nf">__CFStrHashCharacters</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">bufLen</span><span class="p">,</span> <span class="n">len</span><span class="p">);</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>OK, let&#8217;s see what this line function <code>__CFStrHashCharacters</code> really does.</p>
-
-<p><a id="list3"></a></p>
-
-<figure class='code'><figcaption><span>List 3: __CFStrHashCharacters</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-<span class='line-number'>3</span>
-<span class='line-number'>4</span>
-<span class='line-number'>5</span>
-<span class='line-number'>6</span>
-<span class='line-number'>7</span>
-<span class='line-number'>8</span>
-<span class='line-number'>9</span>
-<span class='line-number'>10</span>
-<span class='line-number'>11</span>
-<span class='line-number'>12</span>
-<span class='line-number'>13</span>
-<span class='line-number'>14</span>
-<span class='line-number'>15</span>
-<span class='line-number'>16</span>
-<span class='line-number'>17</span>
-<span class='line-number'>18</span>
-<span class='line-number'>19</span>
-<span class='line-number'>20</span>
-<span class='line-number'>21</span>
-<span class='line-number'>22</span>
-<span class='line-number'>23</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cm">/* In this function, actualLen is the length of the original string; but len is the number of characters in buffer. The buffer is expected to contain the parts of the string relevant to hashing.</span>
-</span><span class='line'><span class="cm">*/</span>
-</span><span class='line'><span class="n">CF_INLINE</span> <span class="n">CFHashCode</span> <span class="nf">__CFStrHashCharacters</span><span class="p">(</span><span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">uContents</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">len</span><span class="p">,</span> <span class="n">CFIndex</span> <span class="n">actualLen</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="n">CFHashCode</span> <span class="n">result</span> <span class="o">=</span> <span class="n">actualLen</span><span class="p">;</span>
-</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="n">len</span> <span class="o">&lt;=</span> <span class="n">HashEverythingLimit</span><span class="p">)</span> <span class="p">{</span>
-</span><span class='line'> <span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">end4</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="p">(</span><span class="n">len</span> <span class="o">&amp;</span> <span class="o">~</span><span class="mi">3</span><span class="p">);</span>
-</span><span class='line'> <span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">end</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="n">len</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">uContents</span> <span class="o">&lt;</span> <span class="n">end4</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">uContents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">uContents</span><span class="p">);</span> <span class="c1">// First count in fours</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">uContents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextUniChar</span><span class="p">(</span><span class="n">uContents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">uContents</span><span class="p">);</span> <span class="c1">// Then for the last &lt;4 chars, count in ones...</span>
-</span><span class='line'> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
-</span><span class='line'> <span class="k">const</span> <span class="n">UniChar</span> <span class="o">*</span><span class="n">contents</span><span class="p">,</span> <span class="o">*</span><span class="n">end</span><span class="p">;</span>
-</span><span class='line'> <span class="n">contents</span> <span class="o">=</span> <span class="n">uContents</span><span class="p">;</span>
-</span><span class='line'> <span class="n">end</span> <span class="o">=</span> <span class="n">contents</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">contents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">contents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">contents</span><span class="p">);</span>
-</span><span class='line'> <span class="n">contents</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="p">(</span><span class="n">len</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mi">16</span><span class="p">;</span>
-</span><span class='line'> <span class="n">end</span> <span class="o">=</span> <span class="n">contents</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">contents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">contents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">contents</span><span class="p">);</span>
-</span><span class='line'> <span class="n">end</span> <span class="o">=</span> <span class="n">uContents</span> <span class="o">+</span> <span class="n">len</span><span class="p">;</span>
-</span><span class='line'> <span class="n">contents</span> <span class="o">=</span> <span class="n">end</span> <span class="o">-</span> <span class="mi">32</span><span class="p">;</span>
-</span><span class='line'> <span class="k">while</span> <span class="p">(</span><span class="n">contents</span> <span class="o">&lt;</span> <span class="n">end</span><span class="p">)</span> <span class="n">HashNextFourUniChars</span><span class="p">(</span><span class="n">contents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">contents</span><span class="p">);</span>
-</span><span class='line'> <span class="p">}</span>
-</span><span class='line'> <span class="k">return</span> <span class="n">result</span> <span class="o">+</span> <span class="p">(</span><span class="n">result</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">actualLen</span> <span class="o">&amp;</span> <span class="mi">31</span><span class="p">));</span>
-</span><span class='line'><span class="p">}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Let&#8217;s take a close look at this method, the whole thing before <code>return</code> is to calculate a fairly large number <code>result</code>, than add another large value by left shifting <code>result</code>.</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="k">return</span> <span class="n">result</span> <span class="o">+</span> <span class="p">(</span><span class="n">result</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">actualLen</span> <span class="o">&amp;</span> <span class="mi">31</span><span class="p">));</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>Note there are some programming technicals used here, especially an unusual usage of macro:</p>
-
-<p>With the definition of <code>HashNextUniChar</code> as</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-<span class='line-number'>2</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#define HashNextUniChar(accessStart, accessEnd, pointer) \</span>
-</span><span class='line'><span class="cp"> {result = result * 257 + (accessStart 0 accessEnd); pointer++;}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>The code below:</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="n">HashNextUniChar</span><span class="p">(</span><span class="n">uContents</span><span class="p">[,</span> <span class="p">],</span> <span class="n">uContents</span><span class="p">);</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p>will be expanded to:</p>
-
-<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
-</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="p">{</span><span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="mi">257</span> <span class="o">+</span> <span class="n">uContents</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="n">uContents</span><span class="o">++</span><span class="p">;}</span>
-</span></code></pre></td></tr></table></div></figure>
-
-
-<p><strong>That&#8217;s it</strong>.</p>
-
-<h2>Verify the hash function</h2>
-
-<p>So far, we understand how the hash value of a NSString is generated. Since the root function is <code>CFStringHashNSString</code> we can place a symbolic breakpoint on this function and <code>__CFStringHash</code> in Xcode and see what will happen. Note that the core function to calculate the hash value <code>__CFStrHashCharacters</code> in <a href="#list3">List 3</a> is marked <code>CF_INLINE</code> we could not put a breakpoint on this function.</p>
-
-<p><img src="http://TonnyXu.github.com/images/Screen%20Shot%202013-02-10%20at%208.28.54%20PM.png" alt="image of putting a symbolic breakpoint" /></p>
-
-<p>I wrote a simple function to see the differences of 2 functions: <code>-[NSString hash]</code> and <code>CFStringHashNSString</code>.</p>
-
-<p><a id="gist"></a></p>
-
-<div><script src='https://gist.github.com/4749305.js'></script>
-<noscript><pre><code></code></pre></noscript></div>
-
-
-
-
-<div><script src='https://gist.github.com/4749305.js?file=[output.txt]'></script>
-<noscript><pre><code>2013-02-10 21:12:57.704 NSStringHash[25996:303] 3294171457232409974
-2013-02-10 21:12:57.707 NSStringHash[25996:303] -1315859082
-2013-02-10 21:12:57.707 NSStringHash[25996:303] -1315859082
-2013-02-10 21:12:57.707 NSStringHash[25996:303] 2DB73F7CB1919576
-2013-02-10 21:12:57.708 NSStringHash[25996:303] FFFFFFFFB1919576
-2013-02-10 21:12:57.708 NSStringHash[25996:303] FFFFFFFFB1919576
-2013-02-10 21:12:57.708 NSStringHash[25996:303] -8069816457736624960
-2013-02-10 21:12:57.709 NSStringHash[25996:303] -8069816457736624960
-2013-02-10 21:12:57.709 NSStringHash[25996:303] 900240AFFA01E4C0
-2013-02-10 21:12:57.710 NSStringHash[25996:303] 900240AFFA01E4C0</code></pre></noscript></div>
-
-
-<p>If see the output, did you remember I mentioned to differences between 32bit system and 64bit system <a href="#note64">before</a>?</p>
-
-<p>The hash value returned by <code>CFStringHashNSString</code> is actually a 32bit unsigned integer, while <code>-[NSString hash]</code> does return a 64bit unsigned integer in 64bit system. If we look at <a href="#list2">List 2</a> again, we will see that Apple&#8217;s engineer noted that they haven&#8217;t implemented <code>LP64</code> yet.</p>
-
-<p>And the last demo code shows if you calculate the hash value for 2 strings longer than 96 characters but have the same characters in first, middle, last 32 characters, we did get 2 hash codes <strong>exactly the same</strong>.</p>
-
-<h2>Problems left</h2>
-
-<p>But if we put a breakpoint on <code>__CFStringHash</code> function, we will see it does get called when <code>-[NSString hash]</code> is called. And if we call <code>__CFStringHash</code> directly we got the same result as <code>CFStringHashNSString</code>. Looks like the way we pass in the parameter to <code>__CFStringHash</code> is wrong.</p>
-
-<p>Now I&#8217;m not so clear about how get a 64bit long hash value when we call <code>__CFStringHash</code> or <code>CFStringHashNSString</code> directly. If anyone knows, please leave a message.</p>
-]]></content>
- </entry>
-
- <entry>
- <title type="html"><![CDATA[Learning Foundation.framework]]></title>
- <link href="http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework/"/>
- <updated>2012-12-24T19:20:00+09:00</updated>
- <id>http://TonnyXu.github.com/2012/12/24/learning-foundation-dot-framework</id>
- <content type="html"><![CDATA[<p><em>[Last update: 2012-12-24]</em></p>
-
-<p><code>Foundation.framework</code> is the most basic framework we use in almost all the iOS/Mac projects. If you can understand this framework well, it can save you a lot of time. Starting from today, I will try to start a new serial of posts about learning <code>Foundation.framework</code>.</p>
-
-<p><strong>Just bear in mind that as the learning journey goes on, this post will be updated from time to time.</strong></p>
-
-<p>In <a href="#post_organization">the bottom of this post</a>, I organized my posts in different groups, just as you can see in <a href="#class_figure">the figure</a></p>
-
-<h2>Basic Understanding of <code>Foundation.framework</code></h2>
-
-<p>Before we start to study the whole framework, a basic understanding of <code>Foundation.framework</code> is necessary. <code>Foundation.framework</code> is usually referred as <code>Foundation</code>.</p>
-
-<ol>
-<li><code>Foundation</code> is part of Cocoa framework(in OSX), and Cocoa Touch Framework(in iOS). To see what Cocoa is, Apple has a good documentation on <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html#//apple_ref/doc/uid/TP40002974-CH3-SW16">What is Cocoa</a>.</li>
-<li>In OSX, Cocoa includes 2 major frameworks: <code>Foundation</code> and <code>AppKit</code>.</li>
-<li>In iOS, Cocoa Touch includes 2 major frameworks: <code>Foundation</code> and <code>UIKit</code>.</li>
-<li>There are little differences between the <code>Foundation</code> in OSX and iOS, but most of the classes are identical in both platform.</li>
-<li><code>Foundation</code> defines a base layer of classes that can be used for any kind of Cocoa program. Thus <code>Foundation</code> has no relationship to user interface(<code>AppKit</code> or <code>UIKit</code>)</li>
-</ol>
-
-
-<h3>Foundation Design Goal</h3>
-
-<blockquote><ul>
-<li>Define basic object behavior and introduce consistent conventions for such things as memory management, object mutability, and notifications.</li>
-<li>Support internationalization and localization with (among other things) bundle technology and Unicode strings.</li>
-<li>Support object persistence.</li>
-<li>Support object distribution.</li>
-<li>Provide some measure of operating-system independence to support portability.</li>
-<li>Provide object wrappers or equivalents for programmatic primitives, such as numeric values, strings, and collections. It also provides utility classes for accessing underlying system entities and services, such as ports, threads, and file systems.</li>
-</ul>
-
-
-<p>Cocoa applications, which by definition link either against the AppKit framework or UIKit framework, invariably must link against the Foundation framework as well. The class hierarchies share the same root class, NSObject, and many if not most of the AppKit and UIKit methods and functions have Foundation objects as parameters or return values. Some Foundation classes may seem designed for applications—NSUndoManager and NSUserDefaults, to name two—but they are included in Foundation because there can be uses for them that do not involve a user interface.</p></blockquote>
-
-<h3>Foundation Paradigms &amp; Policies</h3>
-
-<blockquote><p>Foundation introduces several paradigms and policies to Cocoa programming to ensure consistent behavior and expectations among the objects of a program in certain situations.:</p>
-
-<ul>
-<li><strong>Object retention and object disposal</strong>. The Objective-C runtime and Foundation give Cocoa programs two ways to ensure that objects persist when they’re needed and are freed when they are no longer needed. <br/>
- <strong>NOTE</strong>: Garbage collection was introduced in Objective-C 2.0 and deprecated since ARC is released. For newly written applications, ARC is recommended. Even the old applications with explicit retain/release paradigm can be upgraded to use ARC.</li>
-<li><strong>Mutable class variants</strong>. Many value and container classes in Foundation have a mutable variant of an immutable class, with the mutable class always being a subclass of the immutable one. If you need to dynamically change the encapsulated value or membership of such an object, you create an instance of the mutable class. Because it inherits from the immutable class, you can pass the mutable instance in methods that take the immutable type. For more on object mutability, see “<a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW33">Object Mutability</a>.”</li>
-<li><strong>Class clusters</strong>. A class cluster is an abstract class and a set of private concrete subclasses for which the abstract class acts as an umbrella interface. Depending on the context (particularly the method you use to create an object), an instance of the appropriate optimized class is returned to you. <code>NSString</code> and <code>NSMutableString</code>, for example, act as brokers for instances of various private subclasses optimized for different kinds of storage needs. Over the years the set of concrete classes has changed several times without breaking applications. For more on class clusters, see “<a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34">Class Clusters</a>.”</li>
-<li><strong>Notifications</strong>. Notification is a major design pattern in Cocoa. It is based on a broadcast mechanism that allows objects (called observers) to be kept informed of what another object is doing or is encountering in the way of user or system events. The object originating the notification can be unaware of the existence or identity of the observers of the notification. There are several types of notifications: synchronous, asynchronous, and distributed. The Foundation notification mechanism is implemented by the <code>NSNotification</code>, <code>NSNotificationCenter</code>, <code>NSNotificationQueue</code>, and <code>NSDistributedNotificationCenter</code> classes. For more on notifications, see “<a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW7">Notifications</a>.”</li>
-</ul>
-</blockquote>
-
-<h2>Know the <code>Foundation</code> Classes</h2>
-
-<p>There are many classes and functions defined in <code>Foundation</code>. Understanding those classes one by one is key to accelerate your development of OSX/iOS apps.</p>
-
-<p>Apple&#8217;s development document already have some very good diagrams, which divided those classes into different groups by functionality.</p>
-
-<h3><a id="class_figure"></a>Figure: The Foundation class hierarchy</h3>
-
-<p><img src="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/objc_foundation_A.jpg" alt="" />
-<img src="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/objc_foundation2_A.jpg" alt="" />
-<img src="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/objc_foundation3_A.jpg" alt="" /></p>
-
-<h3>Read the Release Note of Each Version</h3>
-
-<p>Apple is keeping the document correct and up-to-date, but, to be frank, <em>most of the time</em>, the document can not reflect the most recent changes of each version, so reading the release note for each version is very important.</p>
-
-<p>Here are the related release notes:</p>
-
-<ul>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/Cocoa/FoundationOlder.html#//apple_ref/doc/uid/TP30000742">Foundation Release Note</a></li>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_8.html%23//apple_ref/doc/uid/TP40011634-SW24">OSX 10.8 Release Note:Foundation</a></li>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_7.html%23//apple_ref/doc/uid/TP40010355-SW31">OSX 10.7 Release Note:Foundation</a></li>
-<li><a href="https://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_6.html%23//apple_ref/doc/uid/TP40008898-SW32">OSX 10.6 Release Note:Foundation</a></li>
-</ul>
-
-
-<h2><a id="post_organization"></a>The Organization of the Posts</h2>
-
-<p>Since there are so many different classes in the <code>Foundation</code>, it&#8217;s necessary to group my posts as <a href="#class_figure">the figure you see above</a>.</p>
-
-<ol>
-<li>Value Objects</li>
-<li>XML</li>
-<li>Strings</li>
-<li>Collections</li>
-<li>Predicates</li>
-<li>OS Services</li>
-<li>File System</li>
-<li>URL</li>
-<li>Interprocess Communication</li>
-<li>Locking/Threading</li>
-<li>Notifications</li>
-<li>Archiving &amp; Serialization</li>
-<li>ObjC Language Services</li>
-<li>Scripting</li>
-<li>Distributed Objects.</li>
-</ol>
-
-
-<h3>Asking for help</h3>
-
-<p>Since such kind of study is really huge and will cost a long time to archive the goal. I&#8217;m here to ask you to join the journey with me.</p>
-
-<h4>How can you help</h4>
-
-<ol>
-<li>Review the post I write, give me feedback.</li>
-<li>Write posts with me, allow me to post in this site.</li>
-<li>Write sample code with me.</li>
-</ol>
-
-
-<h4>How to Join the Journey</h4>
-
-<ol>
-<li>Twitter: <a href="http://twitter.com/TonnyXu">@TonnyXu</a></li>
-<li>Facebook: <a href="http://facebook.com/Xu.Tonny">Tonny Xu</a></li>
-<li>Email: tonny.xu[AT]gmail.com</li>
-</ol>
-
-
-<hr />
-
-<h2>License - Creative Common 3.0</h2>
-
-<p><a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Loving Cocoa</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://tonnyxu.github.com" property="cc:attributionName" rel="cc:attributionURL">Tonny Xu</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US">Creative Commons Attribution-NonCommercial 3.0 Unported License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="http://tonnyxu.github.com" rel="dct:source">http://tonnyxu.github.com</a>.</p>
-]]></content>
- </entry>
-
- <entry>
<title type="html"><![CDATA[Finished setting up Octopress]]></title>
<link href="http://TonnyXu.github.com/2012/11/21/finished-setting-up-octopress/"/>
<updated>2012-11-21T17:51:00+09:00</updated>
@@ -507,7 +35,7 @@ for his reason and tutorial. Especially when you want to use beautiful math
expressions, read this. Other posts like
<a href="http://akkunchoi.github.com/jekyll-github-blogging.html">akkunchoi</a> who
built his blog system on Jekyll which is the base system of Octopress.
-Another guy <a href="http://blueplanet.github.com/blog/page/8/">BluePlanet</a> moved from Jekyll to Octopress. <a href="http://change-the-world.github.com/blog/2012/08/26/1/">Shun Matsumoto</a> also wrote a post in Japanese. If you&#8217;d like to read Japanese, it&#8217;s a good post.</p>
+Another guy <a href="http://blueplanet.github.com/blog/page/8/">BluePlanet</a> moved from Jekyll to Octopress. <a href="http://change-the-world.github.com/blog/2012/08/26/1/">Shun Matsumoto</a> also wrote a post in Japanese. If you&rsquo;d like to read Japanese, it&rsquo;s a good post.</p>
<p>For me, the reason is simple.</p>
@@ -519,7 +47,7 @@ more fun than HTML or any other markup language</li>
</ol>
-<h2>What&#8217;s next?</h2>
+<h2>What&rsquo;s next?</h2>
<p>Just keep on updating my blogs and bring you high quality posts on
iOS/Mac/Web/HTML5 relates fields.</p>
View
60 blog/archives/index.html
@@ -5,13 +5,12 @@
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
- <title>Blog Archive - Learning Foundation.framework | Tonny Xu</title>
+ <title>Blog Archive - Tonny Xu</title>
<meta name="author" content="Tonny Xu">
- <meta name="description" content="Blog Archive 2013 NSString - Hash function
-Feb 10 2013 posted in NSString, algorithm, hash 2012 Learning Foundation.framework
-Dec 24 2012 posted in &hellip;">
+ <meta name="description" content="Blog Archive 2012 Finished setting up Octopress
+Nov 21 2012 Recent Posts Finished setting up Octopress GitHub Repos Status updating... @TonnyXu on &hellip;">
<!-- http://t.co/dKP3o1e -->
@@ -26,7 +25,7 @@
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
- <link href="/atom.xml" rel="alternate" title="Learning Foundation.framework | Tonny Xu" type="application/atom+xml">
+ <link href="/atom.xml" rel="alternate" title="Tonny Xu" type="application/atom+xml">
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
@@ -49,9 +48,9 @@
<body >
<header role="banner"><hgroup>
- <h1><a href="/">Learning Foundation.framework | Tonny Xu</a></h1>
+ <h1><a href="/">Tonny Xu</a></h1>
- <h2>Life is like a box of chocolates...</h2>
+ <h2>iOS/Mac/Ruby/JS/HTML5 lover. Japan Mobile industry observer</h2>
</hgroup>
@@ -89,41 +88,10 @@ <h1 class="entry-title">Blog Archive</h1>
- <h2>2013</h2>
-
-<article>
-
-<h1><a href="/2013/02/10/nsstring-hash-function/">NSString - Hash function</a></h1>
-<time datetime="2013-02-10T18:40:00+09:00" pubdate><span class='month'>Feb</span> <span class='day'>10</span> <span class='year'>2013</span></time>
-
-<footer>
- <span class="categories">posted in <a class='category' href='/blog/categories/nsstring/'>NSString</a>, <a class='category' href='/blog/categories/algorithm/'>algorithm</a>, <a class='category' href='/blog/categories/hash/'>hash</a></span>
-</footer>
-
-
-</article>
-
-
-
-
<h2>2012</h2>
<article>
-<h1><a href="/2012/12/24/learning-foundation-dot-framework/">Learning Foundation.framework</a></h1>
-<time datetime="2012-12-24T19:20:00+09:00" pubdate><span class='month'>Dec</span> <span class='day'>24</span> <span class='year'>2012</span></time>
-
-<footer>
- <span class="categories">posted in <a class='category' href='/blog/categories/cocoa/'>Cocoa</a>, <a class='category' href='/blog/categories/foundation-framework/'>Foundation.framework</a>, <a class='category' href='/blog/categories/objective-c/'>Objective-C</a></span>
-</footer>
-
-
-</article>
-
-
-
-<article>
-
<h1><a href="/2012/11/21/finished-setting-up-octopress/">Finished setting up Octopress</a></h1>
<time datetime="2012-11-21T17:51:00+09:00" pubdate><span class='month'>Nov</span> <span class='day'>21</span> <span class='year'>2012</span></time>
@@ -144,14 +112,6 @@ <h1 class="entry-title">Blog Archive</h1>
<ul id="recent_posts">
<li class="post">
- <a href="/2013/02/10/nsstring-hash-function/">NSString - Hash function</a>
- </li>
-
- <li class="post">
- <a href="/2012/12/24/learning-foundation-dot-framework/">Learning Foundation.framework</a>
- </li>
-
- <li class="post">
<a href="/2012/11/21/finished-setting-up-octopress/">Finished setting up Octopress</a>
</li>
@@ -245,6 +205,14 @@ <h1 class="entry-title">Blog Archive</h1>
+ <script type="text/javascript">
+ (function() {
+ var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
+ script.src = 'https://apis.google.com/js/plusone.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
+ })();
+ </script>
+
<script type="text/javascript">
View
286 blog/categories/algorithm/atom.xml
@@ -1,286 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<feed xmlns="http://www.w3.org/2005/Atom">
-
- <title><![CDATA[Category: algorithm | Learning Foundation.framework | Tonny Xu]]></title>
- <link href="http://TonnyXu.github.com/blog/categories/algorithm/atom.xml" rel="self"/>
- <link href="http://TonnyXu.github.com/"/>
- <updated>2013-02-11T02:00:23+09:00</updated>
- <id>http://TonnyXu.github.com/</id>
- <author>
- <name><![CDATA[Tonny Xu]]></name>
-
- </author>
- <generator uri="http://octopress.org/">Octopress</generator>
-
-
- <entry>
- <title type="html"><![CDATA[NSString - Hash function]]></title>
- <link href="http://TonnyXu.github.com/2013/02/10/nsstring-hash-function/"/>
- <updated>2013-02-10T18:40:00+09:00</updated>
- <id>http://TonnyXu.github.com/2013/02/10/nsstring-hash-function</id>
- <content type="html"><![CDATA[<h2>The origin of <code>-[NSString hash]</code> function</h2>
-
-<p>The <code>-[NSString hash]</code> function is actually defined by NSObject protocol(not the class), it defines <code>-[NSObject hash]</code> as requir