技术宅,fat-man

增加语言的了解程度可以避免写出愚蠢的代码

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

hello,world不使用ARC

main.m

复制代码
//
//  main.m
//  Hello
//
//  Created by lishujun on 14-8-28.
//  Copyright (c) 2014年 lishujun. All rights reserved.
//

#import <UIKit/UIKit.h>

// 视图控制器对象
@interface HelloWorldViewController : UIViewController
@end

@implementation HelloWorldViewController

-(void) loadView
{
    /*
     self.view = contentView; 
     [self setView contentView];
     等价,设置属性其实是调用set方法
     */
    
    //创建视图对象
    UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    NSLog(@"new contentView : %d", [contentView retainCount]);  // new contentView : 1
    contentView.backgroundColor = [UIColor lightGrayColor];
    NSLog(@"set contentView : %d", [contentView retainCount]);  // set contentView : 1
    [self setView: contentView];
     NSLog(@"add contentView : %d", [contentView retainCount]); // add contentView : 2
    
    //创建label对象
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
    NSLog(@"new label : %d", [label retainCount]);              // new label : 1
    label.text = @"Hello World";
    label.center = contentView.center;
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor redColor];
    NSLog(@"set label : %d", [label retainCount]);              // set label : 1
    
    //在视图上添加label
    [contentView addSubview:label];
    NSLog(@"add label : %d", [label retainCount]);              // add label : 2
    [label release];
     NSLog(@"release label : %d", [label retainCount]);         // release label : 1
    
}

@end


// 委托对象
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>
{
    IBOutlet UIWindow *window;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) HelloWorldViewController *viewController;
//window 必须声明为属性,声明为局部变量则无法绘制视图,显示为黑屏
//apple 官方文档把viewController也声明为属性了
@end

@implementation HelloWorldAppDelegate

@synthesize window;
@synthesize viewController;

-(void) applicationDidFinishLaunching:(UIApplication *)application
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.viewController = [[HelloWorldViewController alloc]init];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
}

@end

// 程序入口
int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");
    }
}
复制代码

输出:

2014-08-30 11:47:30.980 HelloNOARC[562:60b] new contentView : 1
2014-08-30 11:47:30.983 HelloNOARC[562:60b] set contentView : 1
2014-08-30 11:47:30.984 HelloNOARC[562:60b] add contentView : 2
2014-08-30 11:47:30.985 HelloNOARC[562:60b] new label : 1
2014-08-30 11:47:30.986 HelloNOARC[562:60b] set label : 1
2014-08-30 11:47:30.986 HelloNOARC[562:60b] add label : 2
2014-08-30 11:47:30.987 HelloNOARC[562:60b] release label : 1

 

posted on   codestyle  阅读(275)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示