Objective--C Practice and source code

Qustion:(MRC下)

1、定义一个Computer类

实例变量:float width; NSString *name;

方法:一个带两个参数的初始化函数;print()函数;dealloc()函数;

2、定义一个Person类

实例变量:NSString *name; Computer *c; int age;

方法:一个带三个参数的初始化函数;print()函数;dealloc()函数;

要求:用该类创建出来的对象能实现copy和存入文件。

3、主函数要求:

a)分别创建Computer类的对象cc和Person类对象pp;并输出对象信息。

b)利用copy函数创建Person类对象p1; 并输出对象信息

c)把pp存入文件hello.txt中,文件目录为Documents目录。

d)从文件hello.txt中读取对象p2, 并输出对象信息。

e)不能出现内存泄漏和多次删除。

---------------------------------------------------------------------------------------------------------

Source code:

  1 #import <Foundation/Foundation.h>
  2
  3 @interface Computer : NSObject <NSCoding>
  4
  5 @property (nonatomic, assign) float width;
  6 @property (nonatomic, retain) NSString *name;
  7
  8 - (id)initWithWidth:(float)w andName:(NSString *)n;
  9
 10 - (void)print;
 11
 12 @end
 13
 14 #import "Computer.h"
 15
 16 @implementation Computer
 17
 18 - (id)initWithWidth:(float)w andName:(NSString *)n
 19 {
 20     if (self = [super init])
 21     {
 22         _width = w;
 23         self.name = n;
 24     }
 25     return self;
 26 }
 27
 28 - (void)print
 29 {
 30     NSLog(@"Width = %f, Name = %@", _width, _name);
 31 }
 32 //编码
 33 - (void)encodeWithCoder:(NSCoder *)aCoder
 34 {
 35     [aCoder encodeObject:[NSNumber numberWithFloat:_width] forKey:@"1"];
 36     [aCoder encodeObject:_name forKey:@"2"];
 37 }
 38 //解码
 39 - (id)initWithCoder:(NSCoder *)aDecoder
 40 {
 41     if (self = [super init])
 42     {
 43         _width = [[aDecoder decodeObjectForKey:@"1"] floatValue];
 44         self.name = [aDecoder decodeObjectForKey:@"2"];
 45     }
 46     return self;
 47 }
 48
 49 - (void)dealloc
 50 {
 51     self.name = nil;
 52     [super dealloc];
 53 }
 54
 55 @end
 56
 57 #import <Foundation/Foundation.h>
 58
 59 @class Computer;
 60 @interface Person : NSObject <NSCoding, NSCopying>
 61
 62 @property (nonatomic, assign) int age;
 63 @property (nonatomic, retain) NSString *name;
 64 @property (nonatomic, retain) Computer *c;
 65
 66 - (id)initWithAge:(int)a andName:(NSString *)n andComputer:(Computer *)cc;
 67
 68 - (void)print;
 69
 70 @end
 71
 72 #import "Person.h"
 73 #import "Computer.h"
 74
 75 @implementation Person
 76
 77 - (id)copyWithZone:(NSZone *)zone
 78 {
 79     Person *p = [[Person allocWithZone:zone] initWithAge:_age andName:_name andComputer:_c];
 80     return p;
 81 }
 82
 83 - (void)encodeWithCoder:(NSCoder *)aCoder
 84 {
 85     [aCoder encodeObject:_name forKey:@"1"];
 86     [aCoder encodeObject:_c forKey:@"2"];
 87     [aCoder encodeObject:[NSNumber numberWithInt:_age] forKey:@"3"];
 88 }
 89
 90 - (id)initWithCoder:(NSCoder *)aDecoder
 91 {
 92     if (self = [super init])
 93     {
 94         self.name = [aDecoder decodeObjectForKey:@"1"];
 95         self.c = [aDecoder decodeObjectForKey:@"2"];
 96         _age = [[aDecoder decodeObjectForKey:@"3"] intValue];
 97     }
 98     return self;
 99 }
100
101 - (id)initWithAge:(int)a andName:(NSString *)n andComputer:(Computer *)cc
102 {
103     if (self = [super init])
104     {
105         _age = a;
106         self.name = n;
107         self.c = cc;
108     }
109     return self;
110 }
111
112 - (void)print
113 {
114     NSLog(@"Age = %d, Name = %@, Computer = %@", _age, _name, _c);
115 }
116
117 - (void)dealloc
118 {
119     self.name = nil;
120     self.c = nil;
121
122     [super dealloc];
123 }
124
125 @end
126
127 #import <Foundation/Foundation.h>
128 #import "Computer.h"
129 #import "Person.h"
130
131 int main(int argc, const char * argv[]) {
132     @autoreleasepool {
133         Computer *c = [[Computer alloc] initWithWidth:20.5 andName:@"Apple"];
134         [c print];
135
136         Person *p = [[Person alloc] initWithAge:20 andName:@"xiaoming" andComputer:c];
137         [p print];
138
139         Person *p1 = [p copy];
140         [p1 print];
141
142         NSString *path = @"/Users/hskj/Documents/hello.txt";
143         [NSKeyedArchiver archiveRootObject:p toFile:path];
144         Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
145         [p2 print];
146
147         [p1 release];
148         [p release];
149         [c release];
150     }
151     return 0;
152 }
时间: 2024-08-26 12:24:53

Objective--C Practice and source code的相关文章

Tips for newbie to read source code

作者:ll kid链接:https://zhuanlan.zhihu.com/p/23796485来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. This post is first posted on my WeChat public account: GeekArtT Reading source code is always one big part for software engineers. Just like the writers to learn

CRC32 Source Code

/* The Quest Operating System * Copyright (C) 2005-2010 Richard West, Boston University * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Softwar

【开源】海看源代码统计工具 Haikan Source Code Counter

Haikan Source Code Counter 海看源代码统计工具 BY 杭州海看网络科技有限公司 ------------------- github上的地址: https://github.com/haikanwhf/HaikanSourceCodeCounter ------------------ 海看源代码统计工具V1.7.rar

About building ant & install ant on centos7 {ant source code 1.94}

? ? ? ? ? ? ? hamcrest-junit-2.0.0.0.jar java-hamcrest-2.0.0.0.jar ? copy to ant-sourceCodeDir/lib/optimal ? ? ? ? About building ant & install ant on centos7 {ant source code 1.94}

退役笔记一#MySQL = lambda sql : sql + &#39; Source Code 4 Explain Plan &#39;

Mysql 查询执行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.prepare 生成逻辑查询plan(sql_optimizer.cc) >># JOIN.optimize 生成物理查询plan(sql_planner.cc) run the explain plan(sql_executor.cc) 退役笔记一#MySQL = lambda sql : sql

Notepad++如何安装并使用source code pro 字体 转

http://blog.yucanlin.cn/2015/04/08/linux-%E5%AE%89%E8%A3%85-source-code-pro-%E5%AD%97%E4%BD%93/ http://dreamume.blog.163.com/blog/static/18492371920136515710747/ 第一步: 下载并解压source code pro安装包 第二步: 安装字体,双击.ttf文件,并选择安装. 第三步: 选择箭头所指项. 注意:一定要选择使用全局字体,和使用全

C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案

C#调试含有源代码的动态链接库遇见there is no source code available for the current location提示时的解决方案: 1.首先试最常规的方法:Clean and then rebuild solution,但是没有解决 2.进入Tools>Options,选择Debugging>General 却掉 Enable address-level debugging 选项,在去掉 Require source files to exactly ma

android activity 启动过程分析(source code 4.4)

说实话,android source code从2.3到4.4变化是蛮多的,尤其是media部分,虽然总的框架是没有多大变化,但是找起代码来看还是挺麻烦的.在android里面最受伤的是使用了java,jni,jvm,Nativity c++等等,各种设计模式横行,当然在学习源码过程中也意识了编程语言基础,数据结构,设计模式的重要性. android source code 经典的地方: 1. 大量使用了各种设计模式如单例模式,装饰模式,工程工厂模式,适配器模式等等. 2. 使用了binder驱

Google Chrome Source Code Download)

Google Chrome 源码下载地址 (Google Chrome Source Code Download) 地址: http://www.xiaohui.com/dev/vccool/internet/google-chrome-source-code-download.htm 1. Google Chrome 源码 SVN 地址:http://src.chromium.org/svn.包含有 Chrome.Gears.Webkit.GCC 等源码以及编译依赖工具.Chrome 浏览器项