Objc – Memory Management基本内容管理
基于C的东西似乎都要这样,要手动去管理内存,Objc也是如此,本人新手,弄一段别人写的指南.
Learned from http://cocoadevcentral.com/d/learn_objectivec/:
Objective-C's memory management system is called reference counting. All you have to do is keep track of your references, and the runtime does the actual freeing of memory.
In simplest terms, you alloc an object, maybe retain it at some point, then send one release for each alloc/retain you sent. So if you used alloc once and then retain once, you need to release twice.

That's the theory of reference counting. But in practice, there are usually only two reasons to create an object:
一般只有2个原因才会去建立一个Object
1. To keep it as an instance variable //作为一个实例变量
2. To use temporarily for single use inside a function //在一个函数里临时使用
In most cases, the setter for an instance variable should just autorelease the old object, and retain the new one. You then just make sure to release it in dealloc as well.
大部分情况下,set一个实例变量的时候我们需要autorelease原来的Object,并且retain新的值。
然后你只需要在dealloc函数里release掉就可以了。
So the only real work is managing local references inside a function. And there's only one rule: if you create an object with alloc or copy, send it a release or autorelease message at the end of the function. If you create an object any other way, do nothing.
所以呢,实际上要做的只是管理函数中的引用。只有一个规则,如果你用alloc或者copy方法建立了一个Object,在函数结尾使用release或者autorelease就可以了。
如果你是用的别的方法建立的,那更简单,什么都不用做~Yes!Just do nothing。
哇靠,真累,不翻译了……反正翻译的也不准,,~
Here's the first case, managing an instance variable:
- (void) setTotalAmount: (NSNumber*)input {
[totalAmount autorelease];
totalAmount = [input retain];
}
- (void) dealloc {
[totalAmount release];
[super dealloc];
}
Here's the other case, local references. We only need to release the object created with alloc:
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75]; NSNumber* value2 = [NSNumber numberWithFloat:14.78]; // only release value1, not value2 [value1 release];
And here's a combo: using a local reference to set an object as an instance variable:
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75]; [self setTotal:value1]; NSNumber* value2 = [NSNumber numberWithFloat:14.78]; [self setTotal:value2]; [value1 release];
Notice how the rules for managing local references are exactly the same, regardless of whether you're setting them as instance variables or not. You don't need to think about how the setters are implemented.
If you understand this, you understand 90% of what you will ever need to know about Objective-C memory management.
Objc-Special Type In Objc
唉,完蛋了,最近忙的晕头转向,都没时间去想找什么好玩的东西放Blog上了……OK,OK,就转载个Objc的教材嘛。
原文作者为Greg Miller,文章地址在:http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html
一下内容来自:http://cocoachina.com/newbie/basic/2009/0611/170.html
Objective-C中有一些很有趣的数据类型经常会被错误地理解。他们中的大多数都可以在/usr/include/objc/objc.h或者这个目录中的其他头文件中找到。下面是从objc.h中摘录的一段,定义了一些数据类型:
// 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
转-10 Useful & Quality Design Resources on GraphicRiver
Learned from http://www.webappers.com/2009/07/28/10-useful-quality-design-resources-on-graphicriver/
回到家才发现没有更新博客,可是今天又没有什么东西可以更新的,,只好转载GR里的内容...
At GraphicRiver you can buy and sell royalty free layered Adobe Photoshop Files, Vector Graphics, Icon Sets and Add-ons for Adobe Photoshop and Illustrator. It’s like having an entire graphics department at your fingertips!
Files are priced from just $1, based on the complexity, quality and use of the file. We have compiled a list of high quality design resources you may find useful when designing your own websites. Although they are not free, some of them are really a real bargain at such low prices.
1) Premium Download Buttons
2) Web 2.0 Styled Slider Boxes
Flash – 说是XXX的模糊理论视觉模型?
是这样的,在AS3天地会看到个帖子,说HTML5会不会是Flash终结者,在里边刚好看到一个模糊理论模型的东西,点进去看了下,是用JS实现的,,地址在这里了:Canvas Stuff。
JS和Flash是非常的像,SO,拷贝一下~来来看看Demo:
Flash-Setter & Getter
在类里边,一些变量会需要get和set方法,而常用的解决方法有两种,一种是自己写get和set函数,另一种是用set和get方法,2个看起来差不多,例子如下:
package {
public class Person{
private var _name:String;
private var _sex:String;
function Person(name:String){
this._name = name;
}
public function setSex(sex:String):void{
this._sex = sex;
}
public function getSex():String{
return _sex;
}
}
}
我们建立了一个Person类,对外提供了函数setSex()和getSex(),外部使用的时候调用这2个函数就可以了.如:'person.setSex("male")','person.getSex()'....
而使用set和get方法则是这样:
package {
public class Person{
private var _name:String;
private var _sex:String;
function Person(name:String){
this._name = name;
}
public function set sex(sex:String):void{
this._sex = sex;
}
public function get sex():String{
return _sex;
}
}
}
这样一来的话就可以直接使用"person.sex"来set和get _sex的值了.
本来一直用的是第1个方案,后来发现第2个方法更简单直观,而且很多第3方类都这么写嘛~
Adobe Wave
似乎是刚放出来不久,官方地址:http://labs.adobe.com/technologies/wave/
介绍:
Adobe® Wave™ is an Adobe AIR® application and Adobe hosted service that work together to enable desktop notifications for web publishers, large and small. It helps publishers stay connected to your community and lets users avoid the email clutter of dozens of newsletters and social network update messages. Adobe Wave lets you reach users directly on their desktop: there is no need to make them download a custom application or build it yourself. If you have a community of people interested in hearing updates about your website, service or business, Adobe Wave is for you.
Adobe Wave allows consumers to opt-in to receive desktop notifications from their favorite websites. Unlike news feeds and content syndication, each notification carries a call-to-action: clicking directs users to a publisher-specified URL, helping to drive traffic back to your site.
Notifications can bring activity and events on your site to users even when they’re not in the browser and not thinking about you. You can specify a URL for each notification, so when a user clicks, they’re taken right where they need to go. Unlike e-mail, Adobe Wave has been architected to be a more trusted communications channel and consequently minimizes spam and phishing concerns.
Notifications can be customized with your logo, background and foreground colors, and a thumbnail image to help your brand shine through to the user.
WordPress – .htaccess的10个绝佳应用
内容转载自:< .htaccess在wordpress中的10个绝佳应用>:
.htaccess文件可以控制Apache Webserver,它非常实用我们可以通过它来实现很多功能。在这篇文章中我们可以看到通过修改.htaccess文件来增强wordpress的功能、安全性、可用性.
警告:在修改.htaccess文件时请确保已经备份以防不测
1.重定向Wordpress默认Rss地址到Feedburner
Feedburner是Google提供的Feed托管服务,非常优秀,如果不是受到国内“墙”的影响Feedsky也不会在国内一家独大。如果我们想手动修改wordpress文件来实现Feedburner替代默认Rss,过程非常繁琐,不过现在没有那么麻烦了,我们可以通过修改.htaccess来轻松实现该功能.
# temp redirect wordpress content feeds to feedburner
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.feedburner.com/jomor [R=302,NC,L]
</ifmodule>
使用时别忘了把代码中的Feedburner地址替换为自己的
2.在wordpress的url中去除/category/
wordpress默认的category的永久链接形式是这样的:
http://yourblog.com/category/wordpress
这样看来,category似乎在url中显的多余,我们可以修改.htaccess来把他去除.
RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]
DFdou注:这里也可以使用一个叫Top Level Categories的插件可以解决这个问题,不过要搭配Permalink Redirect Plugin.
修改过后链接就会变成这个样子了:http://yourblog.com/wordpress.
盛世中国-来自人民日报
一觉醒来已经是10点半,赶紧开电脑看直播,阿,只有日食转播了.
正好今天无聊的人很多,发一个全食照片+一个来自中国日报的< 盛世中国>吧:

中国有一个最大的党,是共产党
中国有一个最大的姓,是老百姓
中国有一座最大的城,是众志成城
中国有一颗最大的心,是万众齐心
万众齐心是最高的城
众志成城是最大的真
泰山昆仑立地顶天是咱共产党
长江黄河奔流不息是咱老百姓
中国有一个最大的爱,是仁爱
中国有一个最大的人,是天人
中国拧一股最大的劲,是团结奋进
中国忙一个最大愿景
是和谐太平
和谐太平是最大的爱
天人合一是最大的仁
大美大德龙飞凤舞是咱华夏美
大仁大道与时俱进是咱神州魂
就像那鱼儿恋江海
是共产党与咱老百姓
就像那蜂蝶唱阳春
是咱盛世这大中国
十三亿笑脸满乾坤
Flash-Five3d类学习
早上在Google Reader里看到的一个第三方类,试了下效果不错,就准备去下载个类库,结果发现five3d.mathieu-badimon.com 居然被河蟹了?顺便骂一句,Picasa也被河蟹了,MLGBZ的!GFW,bullshit!
OK,骂完了,先来2个Demo,一个是文本的:
另一个是图形的:
AS3-子弹躲避游戏(下)
接上一篇《AS3-子弹躲避游戏(上)》。
有了Enemy和Role之后,自然就是游戏的主体控制部分了,这里是Game类:
这个类负责生成多少个Enemy,Role如何移动,游戏如何开始和如何结束,以及控制其他事务,比如记时类。
package{
import flash.display.Sprite;
import flash.events.*;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import fl.controls.Button;
import Config;
import Role;
import Enemy;
import TimeCount;
public class Game extends Sprite{
private var _role:Role;
private var _enemy:Sprite;
private var _time:TimeCount;
private var _enemyTotal:uint;
private var _gameFlag:Boolean;
private var startBtn:Button;
private var gameInfo:TextField;
//存储四个方向键是否按下的变量
private var leftArrow:Boolean=false;
private var rightArrow:Boolean=false;
private var upArrow:Boolean=false;
private var downArrow:Boolean=false;
public function Game():void{
startBtn = new Button();
startBtn.label = "StartGame";
startBtn.x=(Config.STAGE_W-startBtn.width)/2;
startBtn.y=(Config.STAGE_H-startBtn.height)/2;
addChild(startBtn);
startBtn.addEventListener(MouseEvent.MOUSE_DOWN,gameStart);
gameInfo = new TextField();
gameInfo.autoSize = TextFieldAutoSize.LEFT;
gameInfo.selectable = false;
gameInfo.text = "上下左右方向键控制方块移动,被黑球击中就挂了。";
gameInfo.x = (Config.STAGE_W-gameInfo.width)/2;
gameInfo.y = startBtn.y-30;
addChild(gameInfo);
}
private function gameStart(_evt:MouseEvent):void{
startBtn.removeEventListener(MouseEvent.MOUSE_DOWN,gameStart);
startBtn.visible = false;
gameInfo.visible = false;
init();
}
private function init():void{
initTime();
initRole();
initEnemy();
}
private function initTime():void{
_time = new TimeCount();
addChild(_time);
}
private function initRole():void{
_role = new Role();
addChild(_role);
_role.x=(Config.STAGE_W-_role.width)/2;
_role.y=(Config.STAGE_H-_role.height)/2;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
_role.addEventListener(Event.ENTER_FRAME, roleMove);
stage.addEventListener(Event.DEACTIVATE, missingStage);//Flash处于非活动状态时调用
}
private function initEnemy():void{
_enemyTotal = Config.ENEMY_TOTAL;
var t:Timer=new Timer(2000,1);
t.addEventListener(TimerEvent.TIMER, timeToCreateEnemy);
t.start();
}
private function createEnemy():void{
var enemy=new Enemy();
enemy.setRole(_role);
_enemy.addChild(enemy);
}
private function getEnemyNum():uint{
return this.numChildren;
}
private function timeToCreateEnemy(_evt:TimerEvent):void{
_enemy = new Sprite();
addChild(_enemy);
for (var i:uint=0; i<_enemytotal ; i++) {
createEnemy();
}
addEventListener(Event.ENTER_FRAME,checkFlag);
}
//按键时的检测,左上右下分别是37,38,39,40
private function keyPressedDown(_evt:KeyboardEvent) {
if (_evt.keyCode==37) {
leftArrow=true;
} else if (_evt.keyCode == 39) {
rightArrow=true;
} else if (_evt.keyCode == 38) {
upArrow=true;
} else if (_evt.keyCode == 40) {
downArrow=true;
}
}
//松开按键时
private function keyPressedUp(_evt:KeyboardEvent) {
if (_evt.keyCode==37) {
leftArrow=false;
} else if (_evt.keyCode == 39) {
rightArrow=false;
} else if (_evt.keyCode == 38) {
upArrow=false;
} else if (_evt.keyCode == 40) {
downArrow=false;
}
}
private function roleMove(_evt:Event) {
//移动的速度
var speed:Number=Config.ROLE_SPEED;
if (leftArrow) {
//MC的位置检测,下边相同
if (0<=_role.x) {
_role.x-=speed;
}
}
if (rightArrow) {
if (_role.x<=Config.STAGE_W-_role.width) {
_role.x+=speed;
}
}
if (upArrow) {
if (0<=_role.y) {
_role.y-=speed;
}
}
if (downArrow) {
if (_role.y<=Config.STAGE_H-_role.height*2) {
_role.y+=speed;
}
}
}
//Flash处于非激活状态时
private function missingStage(_evt:Event) {
leftArrow=false;
rightArrow=false;
upArrow=false;
downArrow=false;
}
private function checkFlag(_evt:Event):void{
//trace(_gameFlag);
if(_gameFlag){
_gameFlag = false;
gameOver();
}
}
private function gameOver(){
removeEventListener(Event.ENTER_FRAME,checkFlag);
_time.stopTime();
gameInfo.text = "You Failed At " + _time.getTime() + " Second!";
var format:TextFormat = new TextFormat();
format.font = "Arial";
format.color = 0xFF0000;
format.size = 30;
gameInfo.setTextFormat(format);
gameInfo.x=(Config.STAGE_W-gameInfo.textWidth)/2;
gameInfo.y=(Config.STAGE_H-gameInfo.textHeight)/2;
gameInfo.visible = true;
startBtn.visible = true;
startBtn.y = gameInfo.y + 40;
startBtn.addEventListener(MouseEvent.MOUSE_DOWN,gameStart);
//_role.roleClear();
_role.removeEventListener(Event.ENTER_FRAME, roleMove);
removeChild(_role);
while(_enemy.numChildren>0){
Enemy(_enemy.getChildAt(0)).enemyClear();
_enemy.removeChildAt(0);
}
removeChild(_enemy);
}
public function setGameFlag(_value:Boolean):void{
_gameFlag = _value;
}
}
}
WordPress-wp_mail()函数
有时候在自定义主题的时候,会用到发邮件的功能,这个时候可能就需要用到这个函数了。
wp_mail()函数是WP用来发邮件的一个函数,类似于PHP的mail(),函数定义如下:
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ){
// Compact the input, apply the filters, and extract them back out
extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
if ( !is_array($attachments) )
$attachments = explode( "\n", $attachments );
global $phpmailer;
// (Re)create it, if it's gone missing
if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer();
}
// Headers
if ( empty( $headers ) ) {
$headers = array();
} else {
//...下边省略..............
//具体文件在WP根目录下wp-mail.php
$to是要发送的email地址,$subject是主题,$message是邮件内容,$headers比较复杂,可以这样设置
$headers = 'From: '. get_option('blogname') .' < ' . get_option('admin_email') . '>';
//尖括号前边有个空格,不要忘记了!
get_option('blogname')就是博客名了,get_option('admin_email')是admin的邮箱地址。还可以设置content-type,charset等参数。
当然了,需要使用wp_mail()函数,就需要成为WP的模板页(这个做法比较方便),或者手动导入全部需要的类文件(不推荐)。
参考资料:《Phpmailer的用法》。
jQuery Plugins-jLook & niceForm
介绍两款Form美化插件,一个是jLook,一个是niceForm,具体两个都很简单,下载插件,然后配置好路径就可以了.
有兴趣的可以看下来自cssrain.cn的jLook demo,简单的几行代码就可以搞定:
<script type="text/javascript">
$(document).ready(function(){
$("#testForm").jLook();
});
</script>
jLook下载地址.
接着是niceForm的demo.
以及下载地址.
AS3 Code Optimization一些代码优化的问题
Learned from http://www.insideria.com/2009/04/51-actionscript-30-and-flex-op.html
1.首先是Array:
建议使用var a = []; 而不是var a = new Array();
2.最快的数组复制方式:
var copy : Array = sourceArray.concat();
记得曾经有人做过测试,的确是concat速度最快。
3.数组的赋值速度不如从别的数组获得值:
employees.push(employee);
employees[2] = employee;
速度不如var employee : Employee = employees[2];
4.使用const 来定义常量
public const APPLICATION_PUBLISHER : String = "Company, Inc.";
5.如果一个类不会再被继承,要使用final前缀:
public final class StringUtils

