[object-c 2.0 程序设计]object-c file handle (二)

时间:2022-08-30 13:06:53
//
// main.m
// cmdTry
//
// Created by Calos Chen on 2017/8/21.
// Copyright © 2017年 Calos Chen. All rights reserved.
// #import <Foundation/Foundation.h>
#import "class4.m"
#import "NSObject_class5.h" @class Fraction;
@interface Fraction : NSObject
{
int numberator;
int denominator;
NSString* name;
@public
double price;
Fraction *myf; }
@property double price;
-(void) print;
-(void) setNumberator: (int)n;
-(void) setName: (NSString*)na;
-(void) setInfo: (int) id name:(NSString*)name; @end @implementation Fraction @synthesize price; -(void) print
{
name= [name getBy:23 andName:@"Ok"];//
NSLog(@"%i it is %@",numberator,name);
}
-(void) setNumberator:(int)n
{
price=5;
numberator=n;
}
-(void)setName:(NSString *)na{
name=na;
} -(void) setInfo:(int)id name:(NSString *)na{
id=id;
name=na;
} @end //---- program section ---- int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSLog(@"who are you!");
}
NSNumber *xdd;
xdd=[NSNumber numberWithInt:45]; NSLog(@"why is it");
Fraction *myf= [[Fraction alloc] init];
[myf setName:@"calos"];
[myf print];
double price=myf->price; int a1=5;
float a2=3.23;
double a3=3.44;
char a4='c';
int a5=(int)a2;
for (int b=1; b<10;b++ ) {
a5+=b;
}
myf.price=88;
double b1=myf.price;
BOOL isPrime=NO;
NSLog(@"%i %f %e %c %i %c %g %e",a1,a2,a3,a4,a5,isPrime,b1,price);
NSLog(@"end........"); //file write
NSFileManager* fm=[NSFileManager defaultManager];
NSString* fname=@"a.txt";
NSFileHandle* fh=[NSFileHandle new];
NSData* data=nil;
NSCoder* coder=[NSCoder new];
[fm createFileAtPath:fname contents:nil attributes:nil];
if([fm isReadableFileAtPath:fname]){
[fh writeData:data];
}
else{
[[fh initWithCoder:coder] writeData:data];
} // 初始化管理类
NSFileManager * manager = [NSFileManager
defaultManager]; // 路径
NSString * DirectoryPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/desktop/我的文件夹1/我的文件夹2"]; NSError * error =
nil;
if ([manager
createDirectoryAtPath:DirectoryPath
withIntermediateDirectories:NO
attributes:nil
error:&error] !=
YES) {
// NSString * str = [error localizedDescription];
NSLog(@"创建失败");
}else {
// NSString * str = [error localizedDescription];
NSLog(@"创建成功");
} //file handle NSFileHandle *inFile, *outFile;
NSData *buffer;
NSData* d1=[@"sss" dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:@"test.txt" contents:d1 attributes:nil]; //打开testfile.txt文件用于读取操作
inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile.txt"]; if(inFile == nil)
{
NSLog(@"Open of testfile.txt for reading failed!");
return 1;
} //创建一个文件用于写数据(第一次是必要的)
[[NSFileManager defaultManager] createFileAtPath:@"testout.txt" contents:nil attributes:nil] ; //打开testout.txt文件用于写入操作
outFile = [NSFileHandle fileHandleForWritingAtPath:@"testout.txt"]; if(outFile == nil)
{
NSLog(@"Open of testout.txt for writing failed!");
return 2;
} //
[outFile truncateFileAtOffset:0]; //从inFile中读取数据,并将其写入到outFile中
buffer = [inFile readDataToEndOfFile]; [outFile writeData:buffer]; //关闭两个文件
[inFile closeFile];
[outFile closeFile]; return 0;
} //for today: 2017-08-21, I have read and practiced the first 153 pages of the object-c 2.0, now, I have known object-c classes and data types, and how to announce and implement the classes and class members, as well as functions in the class implementation. also property synthesize methods. loops.
//homework: implement a new class, and new interface and fill the class with members and all tangible members, then use all of them. @class declaration. So, write a console program with object-c.

目前可以使用 Object-c 的 file 处理, 可以操作目录,并读写文件, 控制台程序生成了正常的文件,感觉比较欣慰,文件处理也算蛮大的一步了,知道了一些 oc 常用的库处理,在渐渐地变熟练

[object-c 2.0 程序设计]object-c file handle (二)的更多相关文章

  1. document&period;forms&lbrack;0&rsqb;&period;submit object is not a function

    今天在做项目的时候发现了一个问题:document.forms[0].submit object is not a function. 这个问题是在用JavaScript 代码来提交一个表单时发生的. ...

  2. Terminating app due to uncaught exception &&num;39&semi;NSInvalidArgumentException&&num;39&semi;&comma; reason&colon; &&num;39&semi;&ast;&ast;&ast; -&lbrack;&lowbar;&lowbar;NSPlaceholderDictionary initWithObjects&colon;forKeys&colon;count&colon;&rsqb;&colon; attempt to insert nil object from objects&lbrack;0&rsqb;&&num;39&semi;

    报错: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlace ...

  3. &ast; -&lbrack;&lowbar;&lowbar;NSPlaceholderDictionary initWithObjects&colon;forKeys&colon;count&colon;&rsqb;&colon; attempt to insert nil object from objects&lbrack;0&rsqb;’

    错误描述: * -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object fr ...

  4. 原型相关的方法isPrototypeOf、Object&period;getPrototypeOf、hasOwnProperty、Object&period;getOwnPropertyName、Object&period;keys

    在看<高程3>第六章的<面向对象的程序设计>的原型那一节时,有一下5个函数,功能较为接近,但是又都很基础,很重要 所以在此,加以说明,以便日后复习 function Perso ...

  5. Android学习笔记&lowbar;46&lowbar;Android的intent之间Object、List、List&lt&semi;Object&gt&semi;和全局变量数据的传递&lpar;Parcelable Serializable&rpar;

    转http://blog.csdn.net/pku_android/article/details/7456305 一.传递List<String>和List<Integer> ...

  6. List&lt&semi;List&lt&semi;Object&gt&semi;&gt&semi; list &equals; new ArrayList&lt&semi;List&lt&semi;Object&gt&semi;&gt&semi;&lpar;&rpar;&semi; 求回答补充问题 list&period;get&lpar;position&rpar;&period;add&lpar;Object&rpar;&semi;为什么会报错啊我想在对应的list里面添加对象

    public static void main(String[] args){ List<List<Object>> list = new ArrayList<List& ...

  7. PHP json&lowbar;decode object时报错Cannot use object of type stdClass as array

    PHP json_decode object时报错Cannot use object of type stdClass as array php再调用json_decode从字符串对象生成json对象 ...

  8. 自己写的demo。List&lt&semi;HashMap&lt&semi;String&comma;Object&gt&semi;&gt&semi;&equals;new ArrayList&lt&semi;HashMap&lt&semi;String&comma;Object&gt&semi;&gt&semi;

    package com.pb.collection; import java.util.ArrayList; import java.util.HashMap; import java.util.It ...

  9. 关于 warning CS0659&colon;&OpenCurlyDoubleQuote;&ast;&ast;&ast;”重写Object&period;Equals&lpar;object o&rpar;但不重写Object&period;GetHashCode&lpar;&rpar;

    对象相等性和同一性 System.Object 类型提供了以下方法, namespace System { // // 摘要: // 支持 .NET Framework 类层次结构中的所有类,并为派生 ...

随机推荐

  1. 【iCore3 双核心板】例程二十二:LAN&lowbar;UDP实验——以太网数据传输

    实验指导书及代码包下载: http://pan.baidu.com/s/1kTPlJMJ iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  2. Context启动startActivity注意

    intent.setFlags()方法中参数的用例: 很多人使用startActivity时候,会碰到如下的异常:Caused by: android.util.AndroidRuntimeExcep ...

  3. C&plus;&plus; (P103—P154)

    1 任一指针变量本身的数据值得类型都是unsigned long int 2 指针值为0的叫做空指针,为了安全起见,声明指针时最好初始化,哪怕是初始化为空指针 3 一般不能使用不同类型变量的地址来给指 ...

  4. &lbrack;Swust OJ 217&rsqb;--Factor&lpar;数论,类素数表&rpar;

    题目链接:http://acm.swust.edu.cn/problem/0217/ Time limit(ms): 2000 Memory limit(kb): 65535    Descripti ...

  5. 201521123108 《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 Q1.ArrayList代码分析 Q1.1 解释ArrayList的contains源代码 答:源代码如下: public boolean contains(Obj ...

  6. 移动端1px边框实现

    问题描述:移动端iPhone上的1px边框看起来像2px那么粗.问题分析:不同的手机有不同的像素密度,在window对象中有一个devicePixelRatio属性,它可以反应设备的像素与css中的像 ...

  7. 团体程序设计天梯赛&lpar;CCCC&rpar; L3014 周游世界 BFS证明

    团体程序设计天梯赛代码.体现代码技巧,比赛技巧.  https://github.com/congmingyige/cccc_code

  8. python 字符串编码解码和格式化问题

    转自:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868191962 ...

  9. iOS保存gif动态图

    - (void)saveImageToPhotos:(NSData*)gifData { /***注意先倒入库 #import <AssetsLibrary/AssetsLibrary.h&gt ...

  10. sha256sum和 md5sum 命令之间的区别

    Short answer: For verifying ISOs, there is no practical difference, use whichever you want, as long ...