Elton's Blog

Tag: Mac

Mac OS X:如何校验SHA-1

by on 九.28, 2009, under Mac

从网上下载的软件,有时候为了确保文件的一致性,需要对软件进行sha验证,尤其是apple的一些重要升级文件。

使用一下步骤可以进行校验

1. 打开 Terminal.
2. 输入以下命令:

1
$ /usr/bin/openssl sha1 [full path to file]

例如:

1
2
$ /usr/bin/openssl sha1 /Users/test/Documents/1024SecUpd2003-03-03.dmg
SHA1(/Users/test/Documents/1024SecUpd2003-03-03.dmg) =2eb722f340d4e57aa79bb5422b94d556888cbf38
Leave a Comment :, , more...

Apple硬件保修信息查询

by on 九.26, 2009, under Mac

你可能买了很多Apple的硬件产品,但是记不清他们都是什么时候保修到期,该不该买apple care。 你可以通过这个网址进行查询https://selfsolve.apple.com/GetWarranty.do。 输入你的序列号就可以了。

1

2

Leave a Comment :, , , more...

Objective-C中的一些特殊的数据类型

by on 八.26, 2009, under Mac

在Objective-C中,有一些我们之前并不熟悉但是经常见到的数据类型,比如id、nil、Nil、SEL等等。在很多文章里,我们都见过这些数据类型的介绍,但是都没有说的太清楚。

这篇文章从最底层的定义开始,介绍一下这些类型到底是怎么定义的,这会帮助我们更加深入地了解Objective-C。

原文作者为Greg Miller,文章地址在:

http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html

Objective-C中有一些很有趣的数据类型经常会被错误地理解。他们中的大多数都可以在/usr/include/objc/objc.h或者这个目录中的其他头文件中找到。下面是从objc.h中摘录的一段,定义了一些数据类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// objc.h
typedef struct objc_class *Class;
typedef struct objc_object {
  Class isa;
} *id;
 
typedef struct objc_selector  *SEL;
typedef id (*IMP)(id, SEL, …);
typedef signed char BOOL;
 
#define YES             (BOOL)1
#define NO              (BOOL)0
 
#ifndef Nil
  #define Nil 0   /* id of Nil class */
#endif
 
#ifndef nil
  #define nil 0   /* id of Nil instance */
#endif

我们在这里解释一下它们的细节:
id

id和void *并非完全一样。在上面的代码中,id是指向struct objc_object的一个指针,这个意思基本上是说,id是一个指向任何一个继承了Object(或者NSObject)类的对象。需要注意的是id 是一个指针,所以你在使用id的时候不需要加星号。比如id foo=nil定义了一个nil指针,这个指针指向NSObject的一个任意子类。而id *foo=nil则定义了一个指针,这个指针指向另一个指针,被指向的这个指针指向NSObject的一个子类。
nil

nil和C语言的NULL相同,在objc/objc.h中定义。nil表示一个Objctive-C对象,这个对象的指针指向空(没有东西就是空)。
Nil

首字母大写的Nil和nil有一点不一样,Nil定义一个指向空的类(是Class,而不是对象)。
SEL

这个很有趣。SEL是“selector”的一个类型,表示一个方法的名字。比如以下方法:

-[Foo count] 和 -[Bar count] 使用同一个selector,它们的selector叫做count。

在上面的头文件里我们看到,SEL是指向 struct objc_selector的指针,但是objc_selector是什么呢?那么实际上,你使用GNU Objective-C的运行时间库和NeXT Objective-C的运行运行时间库(Mac OS X使用NeXT的运行时间库)时,它们的定义是不一样的。实际上Mac OSX仅仅将SEL映射为C字符串。比如,我们定义一个Foo的类,这个类带有一个- (int) blah方法,那么以下代码:

1
NSLog (@"SEL=%s", @selector(blah));

会输出为 SEL=blah。

说白了SEL就是返回方法名。
IMP

从上面的头文件中我们可以看到,IMP定义为 id (*IMP) (id, SEL, …)。这样说来, IMP是一个指向函数的指针,这个被指向的函数包括id(“self”指针),调用的SEL(方法名),再加上一些其他参数。

说白了IMP就是实现方法。
Method

在objc/objc-class.h中定义了叫做Method的类型,是这样定义的:

1
2
3
4
5
6
7
typedef struct objc_method *Method;
 
struct objc_method {
  SEL method_name;
  char *method_types;
  IMP method_imp;
};

这个定义看上去包括了我们上面说过的其他类型。也就是说,Method(我们常说的方法)表示一种类型,这种类型与selector和实现(implementation)相关。
Class

从上文的定义看,Class(类)被定义为一个指向struct objc_class的指针,在objc/objc-class.h中它是这么定义的:

1
2
3
4
5
6
7
8
9
10
11
12
struct objc_class {
  struct objc_class *isa;
  struct objc_class *super_class;
  const char *name;
  ong version;
  long info;
  long instance_size;
  struct objc_ivar_list *ivars;
  struct objc_method_list **methodLists;
  struct objc_cache *cache;
  struct objc_protocol_list *protocols;
};
Leave a Comment :, more...

Snow leopard正式版开包图

by on 八.26, 2009, under Mac

据macrumors消息,西班牙苹果fans已收到snow leopard零售版。
并已经证实正式版的snow leopard的版本号就是Build 10A432(传说中的GM版).

Snow leopard正式版开包图

Snow leopard正式版开包图

Leave a Comment :, more...

苹果电脑电池使用方法

by on 八.26, 2009, under Mac

官方网站关于新机器使用时的电池的唯一要求是,要让第一次连接电脑后让电池完全充电,然后再进行软件更新。 就校正电池的方法:

1. 完全充电。
2. 使用电源适配器供电达两个小时,其间可使用电脑。
3. 拆下电源适配器让电池放电,直至屏幕显示电量不足对话栏。其间可使用电脑。
4. 让电脑进入睡眠状态。
5. 关机或让电脑保持睡眠状态达5小时以上。
6. 连上电源适配器,让电脑完全充电。

Leave a Comment : more...

Leopard下手工安装ruby,rails和mysql

by on 八.23, 2009, under Mac, Rails

1. 安装readline
ftp://ftp.gnu.org/gnu/readline/

1
2
3
4
cd readline-6.0
./configure --prefix=/usr/local
make
sudo make install

2. 安装ruby

1
2
3
4
5
6
tar xvjf ruby-1.8.7-p174.tar.bz2
cd ruby-1.8.7-p174
./configure --prefix=/usr/local/ruby --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
sudo make install-doc

使用ruby -v来确认版本安装正确

3. 安装mysql

1
2
3
4
5
6
/configure --prefix=/usr/local/mysql --with-extra-charsets=complex --enable-thread-safe-client --enable-local-infile --enable-shared --with-plugins=max-no-ndb
make
sudo make install
cd /usr/local/mysql
sudo ./bin/mysql_install_db --user=mysql
sudo chown -R mysql ./var/

mac os x 是靠 launchd 守护进程运行的, 配置文件用的是 PropertyList (XML 格式), 通过观察发现每10秒检查一次, 发现进程不在就启动.

方法是新建 /Library/LaunchDaemons/com.mysql.mysqld.plist 写入如下内容.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>KeepAlive</key>
<true />
	<key>Label</key>
	<string>com.mysql.mysqld</string>
	<key>Program</key>
	<string>/usr/local/mysql/bin/mysqld_safe</string>
	<key>RunAtLoad</key>
<true />
	<key>UserName</key>
	<string>mysql</string>
	<key>WorkingDirectory</key>
	<string>/usr/local/mysql</string>
	</dict>
</plist>

加入守护清单
sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

查看清单
launchctl list

解除守护
sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist

4. 安装mysql的c驱动

http://rubyforge.org/fsr/download.php/51087/mysql-ruby-2.8.1.tar.gz

1
2
3
4
5
tar xzvf mysql-ruby-2.8.1.tar.gz
cd mysql-ruby-2.8.1
ruby extconf.rb --with-mysql-dir=/usr/local/mysql
make
sudo make instal

如果出现类似以下的错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... yes
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

就换用

1
sudo ruby extconf.rb --with-mysql-config
7 Comments :, , , more...

iPhone Table View 教程(2) 创建一个简单表格

by on 七.05, 2009, under iPhone

上一节我们介绍了iPhone的table view,这节我们先做一个简单的表格例子。

在Xcode中新建一个view based项目,命名为SimpleTable。

新建xcode项目

新建xcode项目

在SimpleTableViewController.h输入以下代码:

1
2
3
4
5
6
7
#import
 
@interface SimpleTableViewController : UIViewController  {
	IBOutlet UITableView *tblSimpleTable;
	NSArray *arryData;
}
@end

定义一个table view IBOutlet变量和一个数组。

打开SimpleTableViewController.xib文件,进入interface builder。
从Library拖入一个table view控件到主窗口中。

选择File’s Owner

File‘s Owner

File‘s Owner

按cmd + 2 打开 ‘Connections Inspector’

关联表格

关联表格

点击tblSimpleTable边上的小圆圈,拖拽到主屏幕上的table view上,将IBOutlet变量跟Interface Builder中的表格table view关联起来。

在选择主屏幕中的table view,拖拽dataSource和delegate两个变量拖拽到File’s Owner上,指定File‘s Owner所对应的对象(即SimpleTableViewController)为tableview的datasource和delegate。

设置data source和delegate

设置data source和delegate

打开SimpleTableViewController.m,编辑其中内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#import "SimpleTableViewController.h"
 
@implementation SimpleTableViewController
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	arryData = [[NSArray alloc] initWithObjects:@"iPhone",@"Android",@"Plam Pre",@"Windows Mobile",nil];
    [super viewDidLoad];
}
 
- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
	// Release any cached data, images, etc that aren't in use.
}
 
- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}
 
- (void)dealloc {
	[arryData release];
    [super dealloc];
}
 
#pragma mark Table view methods
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arryData count];
}
 
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
 
    // Set up the cell...
	cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
    return cell;
}
 
@end

运行可以得到:

运行结果

运行结果

14 Comments :, , more...

iPhone Table View 教程(1) 基础知识

by on 七.05, 2009, under iPhone

Table View是iPhone最重要的用户UI之一,用来显示具有层级关系的数据结构。本教程将建立一个最基本的Table View,显示数组中的数据。

Table View的角色

  • 让用户在层级化的数据中浏览
  • 展示一个可选择列表选项
  • 展示一个有序的项目列表
  • 在不同的分组中显示详细信息和控件

Table view中的每一行称作一个cell,用来显示文字、图片或者其他内容。每个cell还可以有子视图。iPhone定义了三种默认的子视图

Disclosure indicator

Disclosure indicator

展开指示器(Disclosure indicator) — UITableViewCellAccessoryDisclosureIndicator

用来以table view方式显示下一级数据

Detail disclosure button

Detail disclosure button

详细信息指示器(Detail disclosure button) –UITableViewCellAccessoryDetailDisclosureButton

用来显示一个详细信息视图(detail view)

Check mark

Check mark

选择标记(Check mark) — UITableViewCellAccessoryCheckmark.

用来标记选择了哪一项。 可以是单选或者多选。

Table View的样式

Table view有两种样式,一种是普通型,一种是分组类型。 分组类型中,每一个分组(section)都可以有自己的header和footer。

Table View cell的样式

从iPhone SDK 3.0开始,可以定义每个cell的样式。UIKit定义了四种cell的样式。你也可以定义自己的样式,但是这四种预定义的样式可以满足绝大多数的需求。

默认样式(UITableViewCellStyleDefault)

Default table row style

Default table row style

显示主标题,可以允许有一个图片。

子标题样式(UITableViewCellStyleSubtitle)

UITableViewCellStyleSubtitle

UITableViewCellStyleSubtitle

一个主标题,一个灰色的副标题位于主标题下面,并可安放一个图片

UITableViewCellStyleValue1样式

UITableViewCellStyleValue1

UITableViewCellStyleValue1

主标题位于左侧,副标题以蓝色小字位于右侧,并右对齐。不允许有图片

UITableViewCellStyleValue2样式

UITableViewCellStyleValue2

UITableViewCellStyleValue2

主标题以蓝色显示位于左侧,并且是右对齐。超过宽度的文字以…省略,副标题以黑色字左对齐方式在主标题右侧显示。不能放置图片。iPhone的联系人程序中的列表就是使用这种布局。

Table View

iPhone中的Table View都是UITableView的实例,用来定义表格的样式和行为,而UITableView是UIScrollView的子类。UIScrollView定义了超过屏幕大小的view的滚动行为,而UITableView则重新定义了滚动行为,使其只能垂直滚动。

Data Source 和 Delegate

一个UITableView对象必须具有一个data source 和一个delegate。 根据MVC设计模式,data source负责连通应用程序的数据(即Model)和table view。 delegate则负责table view的外观和行为。 data source 和delegate往往是一个对象,而这个对象通常又是UITableViewController的子类。

data source 遵循 UITableViewDataSource 协议,delegate遵循 UITableViewDelegate 协议

  • UITableViewDataSource 协议有一个可选方法,告诉table view有多少个section(默认是一个)
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView。
    还有一个必须方法,用来告诉table view每个section有多少行(row)
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section。
  • UITableViewDelegate 协议定义了一个必须方法,这个方法返回一个cell,使的table view在画某一行的时候显示。
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • UILocalizedIndexedCollation 是一个辅助类,是iPhone OS 3.0新引进的类,用来帮助data source组织数据,以便于在有序列表(indexed lists)中显示,当用户点击某一个项目的时候,可以正确显示对应的section。 UILocalizedIndexedCollation还可以本地化section的标题。
Leave a Comment :, , more...

给iPhone程序创建Splash欢迎界面

by on 六.23, 2009, under iPhone

看到很多iPhone的程序都有一个欢迎页面,看到网上有篇文章,参考结合自己的实际做了一个例子。

官方SDK最简单的方法

最简单的方法就是做一个全屏的欢迎页的图片,把它命名为Default.png,然后放在Xcode工程的Resource里面。 执行就可以看到你的这个默认图像在程序完全加载之前显示在屏幕上。

但是这个方法有个问题,如果你的程序很快载入了,这个图片会立刻消失,导致还没有看清楚图片上的内容。 而且有些内容虽然程序已经载入了,但是有些程序需要的资源是要从服务器上加载的,所以直接进入程序,用户还是无法使用这个应用。

自定义model view方法

这个方法的大体思路就是创建一个model view,在程序载入完成后调用这个model view,显示其中的图片。当你的资源载入完成后,再完全移除这个model view。 具体方法为:

  1. 随便建立一个iPhone的工程,例如叫Splash。
  2. 在SplashAppDelegate.m中,在applicationDidFinishLaunching方法的最后加入:
  3. 1
    
    [viewController showSplash];

    以在加载程序完成后,显示欢迎页

  4. 在SplashViewController.h加入以下属性和方法
  5. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    #import <uikit/UIKit.h>
     
    @interface SplashViewController : UIViewController {
    	IBOutlet UIView *modelView;
     
    }
     
    - (void) showSplash;
    - (void) hideSplash;
     
    @end

    其中*modelView就是我们要用到的model view,用来显示splash图片的。 另外两个方法是控制显示和隐藏这个splash的

  6. 在SplashController.m加入上面定义的两个方法的实现
  7. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    -(void)showSplash
    {
    	UIViewController *modalViewController = [[UIViewController alloc] init];
     
    	modalViewController.view = modelView;
     
    	[self presentModalViewController:modalViewController animated:NO];
     
    	[self performSelector:@selector(hideSplash) withObject:nil afterDelay:2.0];
     
    }
     
    //hide splash screen
     
    - (void)hideSplash{
     
    	[[self modalViewController] dismissModalViewControllerAnimated:YES];
     
    }

  8. 创建视图
  9. 在Interface builder中给SplashView添加一个view视图,在工具栏里面拖拽一个view视图到Splash VIew中

    view视图

    view视图


    添加view视图

    添加view视图

    链接File’s Owner中的modelview到新建的view上

    链接modelview到新建的view上

    链接modelview到新建的view上

  10. 添加imageView到新建的view上
  11. 拖拽一个imageview到刚建立的view中

    添加imageview

    添加imageview

    选中这个imageview,修改其中的image属性为你指定的图片,我这里选择了之前导入的Default.png

    指定图片

    指定图片

之后你就可以运行了,应该是2秒后,splash图片自动消失。

4 Comments :, , more...

iPhone SDK 3.0 MAPKit使用入门(1) 创建一个MKMapView实例

by on 六.21, 2009, under iPhone

iPhone OS 3.0发布了,MAPKit是其中新增的API,但是Apple并没有公布相应的guide,只有一个相关函数的API。 再网上参考了其他人的例子,试着照猫画虎的做了几个例子。

MAPKit主要的类是MKMapView,它提供了一个嵌入式的地图接口,就像在自带的Maps程序里提供的那样。你可以使用这个类在你的程序中显示地图和操作地图

当你初始化一个map view(MKMapView的实例)的时候,你需要指定一个region(MKCoordinateRegion类型)给这个地图。你可以通过指定map view实例的region属性来设置它的值。region定义了一个中央点和水平和垂直的距离,这个区域显示的大小和比例是根据一个span来调节的。

span(MKCoordinateSpan)定义了指定中央点(point)的map能显示多少内容以及比例尺。 一个大的span的值,将可以展现更多的内容和更小的放大级别,反之则展现更细节的内容和更大的放大级别。

你可以通过设置map view的scrollEnabled 和 zoomEnabled属性来设置是否允许滚动地图和放大缩小地图。

我们先来看一个例子:
创建一个utility application应用程序,在MainViewController.h中引入MapKit/MapKit.h头文件,定义一个MKMapView实体变量

1
2
3
4
5
6
7
8
9
10
#import "FlipsideViewController.h"
#import <mapKit/MapKit.h>
 
@interface MainViewController : UIViewController <flipsideViewControllerDelegate> {
	MKMapView *mapView;
}
 
- (IBAction)showInfo;
 
@end

在MainViewController.m中,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import "MainViewController.h"
#import "MainView.h"
 
 
@implementation MainViewController
 
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
 
 
 
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
	 mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
	 mapView.showsUserLocation=TRUE;
	 [self.view insertSubview:mapView atIndex:0];
 }
 
...

就是初始化了一个MKMapView实例,并将它显示在主视图上。
之后就可以执行了, 你可以看到一个标有你当前位置点的小程序。

4 Comments :, , , , 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...