Elton's Blog

Ruby on Rails: RubyGem version error: rack(1.0.0 not ~> 1.0.1) (RuntimeError)

by Elton on 2010年03月7日, under Rails

当你升级了rails后,有时候会出现rack版本跟rails版本不匹配的情况。 当你执行./script/generate命令的时候,会出现以下错误信息:

  1.  
  2. /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:271:in `require_frameworks‘: RubyGem version error: rack(1.0.0 not ~> 1.0.1) (RuntimeError)
  3.         from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:134:in `process’
  4.         from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `send
  5.         from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `run’
  6.         from /Volumes/Data/study/ruby/blog/config/environment.rb:9
  7.         from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require
  8.         from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
  9.         from /Library/Ruby/Gems/1.8/gems/rails-2.3.5/lib/commands/generate.rb:1
  10.         from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require
  11.         from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
  12.         from ./script/generate:3
  13.  

解决方案:

  1.  
  2. $ sudo gem uninstall rack
  3.  
  4. Select gem to uninstall:
  5.  1. rack-1.0.0
  6.  2. rack-1.1.0
  7.  3. All versions
  8. > 1
  9.  
  10. You have requested to uninstall the gem:
  11.         rack-1.0.0
  12. actionpack-2.3.5 depends on [rack (~> 1.0.0)]
  13. actionpack-2.3.4 depends on [rack (~> 1.0.0)]
  14. If you remove this gems, one or more dependencies will not be met.
  15. Continue with Uninstall? [Yn]  y
  16. Successfully uninstalled rack-1.0.0
  17.  

然后再执行

  1.  
  2. sudo gem install -v=2.3.5 rails
  3.  
  4. Successfully installed rack-1.0.1
  5. 1 gem installed
  6. Installing ri documentation for rack-1.0.1
  7. Installing RDoc documentation for rack-1.0.1
  8.  

问题解决

1 Comment :, , more...

如何使用Objective-C解析HTML和XML

by Elton on 2010年02月25日, under Mac

使用Objective-C解析HTML或者XML,系统自带有两种方式一个是通过libxml,一个是通过NSXMLParser。不过这两种方式都需要自己写很多编码来处理抓取下来的内容,而且不是很直观。

有一个比较好的类库hpple,它是一个轻量级的包装框架,可以很好的解决这个问题。它是用XPath来定位和解析HTML或者XML。

安装步骤:
-加入 libxml2 到你的项目中
Menu Project->Edit Project Settings
搜索 “Header Search Paths”
添加新的 search path “${SDKROOT}/usr/include/libxml2″
Enable recursive option

-加入 libxml2 library 到你的项目
Menu Project->Edit Project Settings
搜索 “Other Linker Flags”
添加新的 search flag “-lxml2″

-将下面hpple的源代码加入到你的项目中:
HTFpple.h
HTFpple.m
HTFppleElement.h
HTFppleElement.m
XPathQuery.h
XPathQuery.m

-XPath学习地址http://www.w3schools.com/XPath/default.asp

示例代码:

  1.  
  2. #import "TFHpple.h"
  3.  
  4. NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"];
  5.  
  6. // Create parser
  7. xpathParser = [[TFHpple alloc] initWithHTMLData:data];
  8.  
  9. //Get all the cells of the 2nd row of the 3rd table
  10. NSArray *elements  = [xpathParser search:@"//table[3]/tr[2]/td"];
  11.  
  12. // Access the first cell
  13. TFHppleElement *element = [elements objectAtIndex:0];
  14.  
  15. // Get the text within the cell tag
  16. NSString *content = [element content];  
  17.  
  18. [xpathParser release];
  19. [data release];
  20.  

另外,还有一个类似的解决方案可以参考
ElementParser http://github.com/Objective3/ElementParser

2 Comments :, , , , , more...

如何在iPhone图标上加提示符(badge)

by Elton on 2010年02月21日, under iPhone

如果你使用Apple iPhone自带的Mail程序,你会注意到,当有新邮件的时候,在这个程序的图标(icon)上会显示对应的新邮件的数字。 如果你的应用程序也想有类似的功能,其实很简单:

  1.  
  2. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  3.     //…
  4.     [application setApplicationIconBadgeNumber:123];
  5. }
  6.  
Leave a Comment :, , more...

将Array、Dictionary等集合类的序列化和反序列化

by Elton on 2010年02月15日, under iPhone

Objective-C的集合类序列化到文件中或者从文件中反序列化其实很简单,请看下面的示例代码:

  1.  
  2. NSArray *array = [NSArray arrayWithObjects:
  3.     @"Hefeweizen", @"IPA", @"Pilsner", @"Stout", nil];
  4.  
  5. NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  6.   array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA",
  7.   @"hoppy", nil];
  8.  
  9. // 得到documents directory的路径
  10. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  11.   NSUserDomainMask, YES);
  12. if ([paths count] > 0)
  13. {
  14.   // Array的保存路径
  15.   NSString  *arrayPath = [[paths objectAtIndex:0]
  16.       stringByAppendingPathComponent:@"array.out"];
  17.  
  18.   // dictionary的保存路径
  19.   NSString  *dictPath = [[paths objectAtIndex:0]
  20.       stringByAppendingPathComponent:@"dict.out"];
  21.  
  22.   // 保存array
  23.   [array writeToFile:arrayPath atomically:YES];
  24.  
  25.   // 保存dictionary
  26.   [dictionary writeToFile:dictPath atomically:YES];
  27.  
  28.   // 从文件中读取回来
  29.   NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
  30.   NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath];
  31.  
  32.   for (NSString *element in arrayFromFile)
  33.     NSLog(@"Beer: %@", element);
  34.  
  35.   for (NSString *key in dictFromFile)
  36.     NSLog(@"%@ Style: %@", key, [dictionary valueForKey:key]);
  37. }
  38.  

输出如下:

Leave a Comment :, , , more...

创建没有按钮的UIAlertView

by Elton on 2010年02月11日, under iPhone

默认的,UIAlertView都有一个或者多个按钮,如果你想创建没有按钮的UIAlertView,可以使用以下的方法:

  1.  
  2. UIAlertView *alert;
  3.  
  4.  
  5. alert = [[[UIAlertView alloc] initWithTitle:@"Configuring Preferences\nPlease Wait…"
  6.   message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
  7.  
  8. [alert show];
  9.  

上面的标题可能有些靠上,可以用过给标题增加回车的方式,使标题居中:

  1.  
  2. UIAlertView *alert;
  3.  
  4.  
  5. alert = [[[UIAlertView alloc] initWithTitle:@"\n\nConfiguring Preferences\nPlease Wait…"
  6.   message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
  7.  
  8. [alert show];
  9.  

如果你还想给UIAlertView添加一个等待提示符,则可以这么做:

  1.  
  2. UIAlertView *alert;
  3.  
  4.  
  5. alert = [[[UIAlertView alloc] initWithTitle:@"Configuring Preferences\nPlease Wait…" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
  6. [alert show];
  7.  
  8. UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  9.  
  10. // Adjust the indicator so it is up a few pixels from the bottom of the alert
  11. indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height – 50);
  12. [indicator startAnimating];
  13. [alert addSubview:indicator];
  14. [indicator release];
  15.  

解除UIAlertView的显示
因为这个AlertView没有按钮,所以就不能通过点击按钮将这个提示框去掉。可以通过程序的方式,将这个窗口关掉。

[alert dismissWithClickedButtonIndex:0 animated:YES];
Leave a Comment :, more...

Three20中给TTPhotoViewController添加播放按钮

by Elton on 2010年02月6日, under iPhone

Three20是一个很不错的iPhone类库,是facebook共享出来的框架。利用TTThumbsViewController可以很容易的制作出类似flickr的应用,实现缩略图和图片查看功能。

但是默认的TTThumbsViewController,点击一个图片的时候,下面的button只有上一个和下一个,并没有播放按钮。 之前以为Three20没有相应的播放功能。但是经过查看代码,发现在TTPhotoViewController中,其实有相关的方法,只是没有把播放按钮显示出来而已。

  1.  
  2. UIBarButtonItem* playButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
  3.     UIBarButtonSystemItemPlay target:self action:@selector(playAction)] autorelease];
  4.   playButton.tag = 1;
  5.  

通过修改TTPhotoViewController大致在400行左右,添加playButton这个变量

  1.  
  2.  _toolbar.items = [NSArray arrayWithObjects:
  3.                    space, _previousButton, space, playButton, space, _nextButton, space, nil];
  4.  

这样重新编译后,就可以看到播放按钮了。

Three20这个库其实还是很实用的,只是文档比较欠缺,没办法,只能直接看源码了。

Leave a Comment :, , , more...

给TableView加背景

by Elton on 2010年02月2日, under iPhone

iPhone默认的表格背景很单调,你可以通过加背景图片的方式来给你的应用增添点亮点。可以通过以下方法来给表格添加背景。
你可以在你的delegate类里面添加如下代码:

  1.  
  2. UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
  3. backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];
  4. [window addSubview:backgroundView];
  5. [backgroundView release];
  6.  
  7. yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];
  8. yourTableViewController.view.backgroundColor = [UIColor clearColor];
  9. [window addSubview:yourTableViewController.view];
  10.  
  11. [window makeKeyAndVisible];
  12.  

ATableViewController是UITableViewController的一个子类

Leave a Comment :, more...

如何使用MyFace快速构建基于JSF的应用

by Elton on 2010年02月2日, under Java

如果大家使用Apache MyFace的JSF实现来搭建JSF应用,可以利用Apache提供的便捷maven方法来快速搭建一个初始的应用。

  1.  
  2. mvn archetype:generate -DarchetypeCatalog=http://myfaces.apache.org
  3. ……
  4. Choose archetype:
  5. 1: http://myfaces.apache.org -> myfaces-archetype-helloworld (Simple Web application using Apache Myfaces)
  6. 2: http://myfaces.apache.org -> myfaces-archetype-helloworld-facelets (Simple Web application using Apache Myfaces and Facelets)
  7. 3: http://myfaces.apache.org -> myfaces-archetype-helloworld-portlets (Simple Web application using Apache Myfaces and Portlets)
  8. 4: http://myfaces.apache.org -> myfaces-archetype-jsfcomponents (Simple JSF Component using Apache Myfaces)
  9. 5: http://myfaces.apache.org -> myfaces-archetype-trinidad (Simple Web application using Apache Myfaces and Trinidad)
  10. Choose a number:  (1/2/3/4/5):
  11. ……
  12.  

可以看到它提供了你5个初始项目进行选择,你可以根据你的需要选择响应的选项。之后填写响应的参数后,你的应用程序框架就生成了。

然后再使用以下命令,来下载必要的依赖包,假设你的groupId=myAppId,artifactId=yourapp

  1.  
  2. cd yourapp
  3. mvn package
  4.  

之后你就搭建了一个初始框架,你可以继续使用maven来操作这个框架。

Leave a Comment :, , , , , more...

千万别study English,应学会learn Englis——英语学习方法强烈推荐

by Elton on 2010年01月29日, under 未分类

l 开始第一阶段:“打通”耳朵,完全听清

要领:

第一, 选择1盘(再次强调是1盘而非一套)适合自身英语水平的磁带;(磁带我选用的是李阳的疯狂英语,电驴有很多资源的,也可用新东方的或者BBC)

第二, 每天集中精力把A面和B面连续听两遍

第三, 要坚持天天听,但每隔6天要休息一天

第四, 直到听清磁带中的所有内容

2 完成第二阶段:听读并举,掌握语法

要领:

第一, 把已经能完全听清的磁带中的第1盘磁带再找出来。

第二, 听写这盘磁带的内容。

第三, 做听写练习时,一句一句地听。即:听完一句后,先按暂停,把刚才听到的写下来,反复听,直到完全听清这句为止。不会的单词根据发音大致拼写出来即可。

第四, 听写完整盘磁带的所有内容后,用英英词典确认不会的单词拼写是否正确(知道其意当然好,不知道也不要紧)。

第五, 按这种方法听写完整盘磁带的内容后,尽量模仿磁带的发音和语调,从头到尾大声朗读(不满意的部分要再听一遍磁带重新朗读)

第六, 感觉到所有的句子都已经能朗朗上口以后,便结束这一阶段。

第七, 整个过程中,每个星期要有1天与英语完全隔绝。

3 突破第三阶段:跃跃欲说,出口成章

要领:

第一, 用英英词典查以前做听写练习不懂的单词。(英英词典,我使用的是《朗文当代英语辞典(第4版)(精装)》,因原来听新东方的视频,新东方老师推荐使用朗文的英英字典)

第二, 把单词的解释和例句记下来,若这其中还有不懂的单词话,继续查单词。

第三, 查词典要坚持查到没有不懂的单词为止。

第四, 大约查1个小时后,暂停查词典,并大声朗读通过查词典整理出的内容。

第五, 朗读约1个小时后结束。

第六, 每周要隔1天。

第七, 查在原文和单词解释中出现的所有不懂的单词、并一直朗读到完全吃透解释和例句为止。

4 征服第四阶段:自我领悟,无典自通

要领:

第一, 准备1盘录像带。(我打算使用MIT的《算法导论》视频)

第二, 带上耳机,每天看1遍。

第三, 能够完全听清之后,便开始听写,朗读。

第四, 将不清楚的单词,利用英英词典查找并朗读。

5 攀登第五阶段:文化融通,渐入佳境

要领:

第一, 准备1张最近的英文原版报纸(在美国发行的报纸)。

第二, 从社会版面挑选一篇短文章(1~2分钟就能念完的),然后大声朗读。必须坚持到完全消化为止,就好像自己成了新闻主持人一样。

第三, 当确信自己不看原文也可以记住文章内容时,把它像讲一个故事一样绘声绘色地复述。

第四, 能够流利地朗读,再选第二篇文章,重复上面所讲的方法。

第五, 看完一个版面后,就像第三阶段那样处理不认识的单词。

第六, 把报纸上广告、名人访谈、漫画等所有内容,都按上述方法加以学习。

下面是书上关于这个英语学习的阐述与分析:
1、分析第一阶段:打通耳朵,完全听清
2、分析第二阶段:听读并举,掌握语法
3、分析第三阶段:跃跃欲说,出口成章
4、分析第四阶段:自我领悟,无典自通
5、分析第五阶段:文化融通,渐入佳境

Leave a Comment : more...

去掉iPhone应用图标上的弧形高光

by Elton on 2010年01月25日, under iPhone

有时候我们的应用程序不需要在图标上加上默认的高光,可以在你的应用的Info.plist中加入:

  1.  
  2. UIPrerenderedIcon,让它等于YES即可
  3.  
1 Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...

Archives

All entries, chronologically...