//
// Book.h
// OCdemo-04-01-shuzu
//
// Created by lanou3g on 15/10/12.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
{
NSString *_bookName;
NSInteger _num;
}
-(void)setBookName:(NSString *)bookName;
-(void)setNum:(NSInteger)num;
-(NSString *)getBookName;
-(NSInteger)getNum;
//初始化方法
-(instancetype)initWithBookName:(NSString *)bookName
num:(NSInteger)num;
//便利构造器
+(Book *)bookWithBookName:(NSString *)bookName
num:(NSInteger)num;
@end
------------------------------------------------------------------------------
//
// Book.m
// OCdemo-04-01-shuzu
//
// Created by lanou3g on 15/10/12.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import "Book.h"
@implementation Book
-(void)setBookName:(NSString *)bookName{
_bookName = bookName;
}
-(void)setNum:(NSInteger)num{
_num = num;
}
-(NSString *)getBookName{
return _bookName;
}
-(NSInteger)getNum{
return _num;
}
//初始化方法
-(instancetype)initWithBookName:(NSString *)bookName
num:(NSInteger)num{
self = [super init];
if (self) {
_bookName = bookName;
_num = num;
}
return self;
}
//便利构造器
+(Book *)bookWithBookName:(NSString *)bookName
num:(NSInteger)num{
Book *book = [[Book alloc] initWithBookName:bookName num:num];
return book;
}
@end
------------------------------------------------------------------------------------------------
//
// Library.h
// OCdemo-04-01-shuzu
//
// Created by lanou3g on 15/10/12.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Book;
@interface Library : NSObject
{
NSString *_admin; //管理员
NSMutableArray *_bookArray; //存放所有图书的数组
}
//添加书籍
-(void)addBookToArray:(Book *)book;
//展示所有图书信息
-(void)showBookList;
//删除书籍
-(void)deleteBook:(Book *)book;
//根据书名查找书籍
-(Book *)selectBookWithBookName:(NSString *)bookName;
//修改图书
-(void)changeBook:(Book *)book
newName:(NSString *)newName;
@end
----------------------------------------------------------------------------------------------------
//
// Library.m
// OCdemo-04-01-shuzu
//
// Created by lanou3g on 15/10/12.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import "Library.h"
#import "Book.h"
@implementation Library
-(instancetype)init{
self = [super init];
if (self) {
//初始化图书数组
_bookArray = [NSMutableArray array];
}
return self;
}
//添加书籍
-(void)addBookToArray:(Book *)book{
[_bookArray addObject:book];
}
//展示所有图书信息
-(void)showBookList{
for (Book *book in _bookArray) {
NSLog(@"%@",[book getBookName]);
}
}
//删除书籍
-(void)deleteBook:(Book *)book{
[_bookArray removeObject:book];
}
//根据书名查找书籍
-(Book *)selectBookWithBookName:(NSString *)bookName{
for (Book *book in _bookArray) {
if ([[book getBookName] isEqualToString:bookName]) {
return book;
}
}
return nil;
}
//修改图书
-(void)changeBook:(Book *)book
newName:(NSString *)newName{
NSInteger index = [_bookArray indexOfObject:book];
[book setBookName:newName];
[_bookArray replaceObjectAtIndex:index withObject:book];
}
@end
-------------------------------------------------------------------------------------------------------------------
//
// main.m
// OCdemo-04-01-shuzu
//
// Created by lanou3g on 15/10/12.
// Copyright (c) 2015年 Object. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Book.h"
#import "Library.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
// NSArray 不可变数组
// 存储 NSObject 对象
//1.创建一个数组
//使用初始化方法
NSArray *array1 = [[NSArray alloc] initWithObjects:@"晓彤", @"石峰",@"jianjian",@"小yi",@"雯琦",nil];
//使用类方法
NSArray *array2 = [NSArray arrayWithObjects:@"见见",@"豪豪",@"帅帅",@"郭隆基",@"lirui", nil];
//使用自面量方法
NSArray *array3 = @[@"haoge",@"yangge",@"boge",@"pengge",@"ruirui",@"xiaoming"];
NSLog(@"%@",array1);
NSLog(@"%@",array2);
NSLog(@"%@",array3);
//2.数组查找
//数组遍历
//通过下标,查找对象
for (int i = 0; i < [array1 count]; i++) {
// NSLog(@"%@",[array1 objectAtIndex:i]);
NSLog(@"%@",array1[i]);
}
//通过对象,查找下标
NSString *str = @"ruirui";
NSUInteger index = [array3 indexOfObject:str];
NSLog(@"%lu",index);
//-------------------------------------------------------------------------------------
// NSMutableArray 可变数组
NSMutableArray *marray1 = [NSMutableArray arrayWithObjects:@"xiaohong",@"xiaofan",@"caicai",@"weige",@"daming",@"mingming" ,nil];
NSLog(@"%@",marray1);
//2.添加、插入元素
//添加
[marray1 addObject:@"dahuang"];
NSLog(@"%@",marray1);
//插入
[marray1 insertObject:@"jinjin" atIndex:0];
NSLog(@"%@",marray1);
//3,删除
//删除指定位置的对象
[marray1 removeObjectAtIndex:0];
NSLog(@"%@",marray1);
//删除指定对象
[marray1 removeObject:@"dahuang"];
NSLog(@"%@",marray1);
//删除最后一个对象
[marray1 removeLastObject];
NSLog(@"%@",marray1);
//删除所有对象
[marray1 removeAllObjects];
NSLog(@"%@",marray1);
//4.替换
// NSMutableArray *mArray2 = @[@"阿鲁",@"任雨生",@"任雨婷",@"进进",@"博文",@"小强"];
NSMutableArray *mArray2 = [NSMutableArray arrayWithObjects:@"阿鲁",@"任雨生",@"任雨婷",@"进进",@"博文",@"小强", nil];
[mArray2 replaceObjectAtIndex:0 withObject:@"lol"];
NSLog(@"%@",mArray2);
for (int i = 0; i < [mArray2 count]; i++) {
NSLog(@"%@",mArray2[i]);
}
//5.交换指定位置的元素
[mArray2 exchangeObjectAtIndex:0 withObjectAtIndex:1];
for (int i = 0; i < [mArray2 count]; i++) {
NSLog(@"%@",mArray2[i]);
}
//另一种遍历方式
for (NSString *str in mArray2) { // 不确定类型 ,写 id
NSLog(@"%@",str);
}
*/
Book *book1 = [[Book alloc] initWithBookName:@"C语言" num:1001];
Book *book2 = [[Book alloc] initWithBookName:@"OC" num:1002];
Book *book3 = [[Book alloc] initWithBookName:@"UI" num:1003];
Library *library = [[Library alloc]init];
[library addBookToArray:book1];
[library addBookToArray:book2];
[library addBookToArray:book3];
//展示所有图书信息
[library showBookList];
//删除书籍
[library deleteBook:book3];
[library showBookList];
//根据书名查找书籍
Book *book4 = [library selectBookWithBookName:@"C语言"];
NSLog(@"name = %@,num = %ld",[book4 getBookName],[book4 getNum]);
//修改图书
[library changeBook:book2 newName:@"Objective-C"];
[library showBookList];
// NSNumber 数值对象
//使用类方法创建对象
NSNumber *number1 = [NSNumber numberWithInt:18];
NSMutableArray *array = [NSMutableArray array];
[array addObject:number1];
NSLog(@"%@",array);
[array addObject:@20];
NSLog(@"%@",array);
//使用字面量方法创建对象
NSNumber *number2 = @30;
[array addObject:number2];
NSLog(@"%@",array);
}
return 0;
}