iOS组对这几周OC语言的学习成果进行检验。
目录
1. 新建一个Singleton单例类
o 用属性关键字给该类写name、age两个属性
o 重写初始化方法,初始化的同时能给name、age两个属性赋值
o 重写description方法(利用方法打印这两个属性值,调用这两个方法打印这两个值)
Singleton类的接口部分如下:
#import <Foundation/Foundation.h>
@interface THGSingleton : NSObject
+ (id)instance;
@end
实现部分如下:
#import "THGSingleton.h"
@implementation THGSingleton
static id instance = nil;
+ (id)instance {
if (!instance) { //instance == nil
instance = [[super alloc] init];
}
return instance;
}
@end
2. 新建一个Person类
o 用属性关键字给该类写name、age两个属性
o 重写初始化方法,初始化的同时能给name、age两个属性赋值
o 重写description方法(利用方法打印这两个属性值,调用这两个方法打印这两个值)
Person类的接口部分如下:
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString* name;
@property int age;
- (id)initWithName: (NSString*)name andAge: (int)age;
@end
实现部分如下:
#import "Person.h"
@implementation Person
- (id)initWithName: (NSString*)name andAge: (int)age {
if (self = [super init]) {
self.name = name;
self->_age = age;
}
return self;
}
- (NSString*)description {
return [NSString stringWithFormat: @"<Person[_name=%@, _age=%d]>", self->_name, self.age];
}
@end
3. 新建一个XiyouMobilePerson类
o 该类继承于Person类
o 该类设置4个成员变量(前两个为int型,后两个为NSString型;四个成员变量依次为iOS、web、andriod、server),将四个成员变量隐藏:同一个类中或子类中才有权访问到
o 设置四个成员变量对应的set / get方法
o 重写ios变量的set方法:如果为奇数则值 +1,偶数则值 -1
XiyouMobilePerson类的接口部分:
#import "Person.h"
@interface XiyouMobilePerson : Person {
@protected
int _iOS;
int _web;
NSString* _android;
NSString* _server;
}
- (void)setIOS: (int)iOS;
- (int)iOS;
- (void)setWeb: (int)web;
- (int)web;
- (void)setAndroid: (NSString*)android;
- (NSString*)android;
- (void)setServer: (NSString*)server;
- (NSString*)server;
@end
实现部分:
#import "XiyouMobilePerson.h"
@implementation XiyouMobilePerson
- (void)setIOS: (int)iOS {
_iOS = (iOS % 2 == 0) ? (_iOS - 1) : (_iOS + 1);
}
- (int)iOS {
return _iOS;
}
- (void)setWeb: (int)web {
_web = web;
}
- (int)web {
return _web;
}
- (void)setAndroid: (NSString*)android {
_android = android;
}
- (NSString*)android {
return _android;
}
- (void)setServer: (NSString*)server {
_server = server;
}
- (NSString*)server {
return _server;
}
@end
4. 新建一个Model类
o 定义一个类型为NSMutableArray的属性,名称为xiyouMobileArray。注意xiyouMoileArray里存放的是XiyouMobilePerson类对象,主要考察属性关键字。
Model类的接口部分:
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property NSMutableArray* xiyouMobileArray;
@end
实现部分:
#import "Model.h"
@implementation Model
@end
5. 定义一个Demand协议
o 协议中声明一个calculate方法:关键字为必须实现
o 协议中声明一个unnecessary方法:关键字为可选实现
o 在XiyouMobilePerson类中遵循这个协议
o 在XiyouMobilePerson类中重写calculate方法:打印iOS与web两个变量相加后的值
定义Demand协议:
#import <Foundation/Foundation.h>
@protocol Demand <NSObject>
@required
- (void)caculate: (int)iOS plusWeb: (int)web;
@optional
- (void)unecessary;
@end
在XiyouMobilePerson类的接口部分中遵循Demand协议;
#import "Person.h"
#import "Demand.h"
@interface XiyouMobilePerson : Person <Demand> {
@protected
int _iOS;
int _web;
NSString* _android;
NSString* _server;
}
- (void)setIOS: (int)iOS;
- (int)iOS;
- (void)setWeb: (int)web;
- (int)web;
- (void)setAndroid: (NSString*)android;
- (NSString*)android;
- (void)setServer: (NSString*)server;
- (NSString*)server;
@end
在XiyouMobilePerson类的实现部分实现caculate方法:
- (void)caculate: (int)iOS plusWeb: (int)web {
NSLog(@"iOS与web两个变量之和为:%d", iOS + web);
}
6. main函数中的操作
o 新建一个Model类对象,在其属性xiyouMobileArray中添加五名xiyouMobilePerson对象的信息
o 信息值随意输入,变量类型匹配即可
o 在上述xiyouMobileArray数组中,打印iOS变量值最大的对象的相关信息
o 删除上述xiyouMobileArray数组中range为 (2,3)的成员
主调函数代码如下:
#import "THGSingleton.h"
#import "Person.h"
#import "XiyouMobilePerson.h"
#import "Model.h"
#import "Demand.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Model* myModel = [[Model alloc] init];
// myModel.xiyouMobileArray = [NSMutableArray arrayWithObjects: [[XiyouMobilePerson alloc] initWithName: @"one" andAge: 1], [[XiyouMobilePerson alloc] initWithName: @"two" andAge: 2], [[XiyouMobilePerson alloc] initWithName: @"three" andAge: 3], [[XiyouMobilePerson alloc] initWithName: @"four" andAge: 4], [[XiyouMobilePerson alloc] initWithName: @"five" andAge: 5], nil];
XiyouMobilePerson* s1 = [[XiyouMobilePerson alloc] initWithName: @"one" andAge: 1];
s1.iOS = 1;
s1.web = 1;
s1.android = @"1";
s1.server = @"1";
XiyouMobilePerson* s2 = [[XiyouMobilePerson alloc] initWithName: @"two" andAge: 2];
s2.iOS = 2;
s2.web = 2;
s2.android = @"2";
s2.server = @"2";
XiyouMobilePerson* s3 = [[XiyouMobilePerson alloc] initWithName: @"three" andAge: 3];
s3.iOS = 3;
s3.web = 3;
s3.android = @"3";
s3.server = @"3";
XiyouMobilePerson* s4 = [[XiyouMobilePerson alloc] initWithName: @"four" andAge: 4];
s4.iOS = 4;
s4.web = 4;
s4.android = @"4";
s4.server = @"4";
XiyouMobilePerson* s5 = [[XiyouMobilePerson alloc] initWithName: @"five" andAge: 5];
s5.iOS = 5;
s5.web = 5;
s5.android = @"5";
s5.server = @"5";
myModel.xiyouMobileArray = [NSMutableArray arrayWithObjects: s1, s2, s3, s4, s5, nil];
NSEnumerator* en = [myModel.xiyouMobileArray objectEnumerator]; //顺序枚举器
XiyouMobilePerson* max = s1;
XiyouMobilePerson* i;
while (i = [en nextObject]) {
if (i.iOS > max.iOS) {
max = i;
}
}
NSLog(@"iOS变量值最大的对象的相关信息:%@ %d %d %d %@ %@", max.name, max.age, max.iOS, max.web, max.android, max.server);
[myModel.xiyouMobileArray removeObjectsInRange: NSMakeRange(2, 3)];
NSLog(@"%@", myModel.xiyouMobileArray);
NSLog(@"%d", [THGSingleton instance] == [THGSingleton instance]);
}
return 0;
}
运行结果如下:
0000000000
客观题
NSString的三种类型及其创建方法方式。
_NSCFConstantString:字符串直接量,是一种编译时常量。
其对象存储在字符串常量区。
使用不同的字符串对象进行创建,当字符序列相同时,其对象的地址也相同,说明字符串直接量是一种单例。
_NSCFString:ta的对象是在运行时创建的一种NSString子类,存储在堆内存上。
和其他对象一样,被创建时获得1引用计数,通过NSString的stringWithFormat等创建的对象一般都是这种类型。
如果字符串大于9或者含有中文或其他特殊符号(可能是非ASCII字符)存在的话会直接称为_NSCFString类型。
NSTaggedPointerString:对于NSString对象来讲,当非字面值常量的数字、英文字母字符串的长度小于等于9的时候会自动成为NSTaggedPointerString类型。
==和isEqual的区别。
==常用来判断两个变量。
两个变量是基本类型,且都是数值类型时,值相等的两个变量使用==就将返回真。两个变量是指针类型时,只有当两个变量保存的地址相同时,才返回真。
NSObject的isEqual方法与==用法一致,但isEqual可以重写,NSString重写了isEqual方法,用于判断用一个类的两个对象包含的字符序列是否相等。(与自己比较返回真,与空比较返回假,不属于同一个类返回假)。
isEqualToString 是 isEqual 的衍生方法,只能用来比较字符串是否相等,而且也是按照字符串的字符是否相等来作为标准的而不是地址。
总结
遗忘知识较多,抓紧学习UI的同时复习OC的重点知识和学长所强调的内容。