Ojbect-C2 6、NSFileHandle 和NSFileManager 的使用

本文深入探讨了文件操作的基础知识,包括如何获取文件句柄、创建文件句柄、文件描述符获取、读写文件内容及使用块进行异步通信。同时,介绍了文件管理器的使用方法,涵盖目录定位、文件创建与删除、移动与复制文件等功能。

Properties

readabilityHandler

The block to use for reading the contents of the file handle asynchronously.

@property (copy) void (^readabilityHandler)( NSFileHandle *);
Discussion

The default value of this property is nil. Assigning a valid block object to this property creates a dispatch source for reading the contents of the file or socket. Your block is submitted to the file handle’s dispatch queue when there is data to read. When reading a file, your handler block is typically executed repeatedly until the entire contents of the file have been read. When reading data from a socket, your handler block is executed whenever there is data on the socket waiting to be read.

The block you provide must accept a single parameter that is the current file handle. The return type of your block should be void.

To stop reading the file or socket, set the value of this property to nil. Doing so cancels the dispatch source and cleans up the file handle’s structures appropriately.

Availability
  • Available in OS X v10.7 and later.
Declared In
NSFileHandle.h

writeabilityHandler

The block to use for writing the contents of the file handle asynchronously.

@property (copy) void (^writeabilityHandler)( NSFileHandle *);
Discussion

The default value of this property is nil. Assigning a valid block object to this property creates a dispatch source for writing the contents of the file or socket. Your block is submitted to the file handle’s dispatch queue when there is room available to write more data. When writing a file, your handler block is typically executed repeatedly until the entire contents of the file have been written. When writing data to a socket, your handler block is executed whenever the socket is ready to accept more data.

The block you provide must accept a single parameter that is the current file handle. The return type of your block should be void.

To stop writing data to the file or socket, set the value of this property to nil. Doing so cancels the dispatch source and cleans up the file handle’s structures appropriately.

Availability
  • Available in OS X v10.7 and later.
Declared In
NSFileHandle.h






NSFILEMANAGER

Tasks

Creating a File Manager

Locating System Directories

Locating Application Group Container Directories

Discovering Directory Contents

Creating and Deleting Items

Moving and Copying Items

Managing ICloud-Based Items

Creating Symbolic and Hard Links

Determining Access to Files

Getting and Setting Attributes

Getting and Comparing File Contents

Converting File Paths to Strings

Managing the Delegate

Managing the Current Directory

Deprecated Methods




<think>我们正在讨论Python中的object类型。根据用户的问题,他们想了解object类型的含义用法。 首先,object是Python中所有类的基类。在Python中,所有类都直接或间接地继承自object。这意味着object提供了一些基本的方法属性,这些方法属性可以被所有Python对象使用。 例如,object类定义了__str__()方法,用于返回对象的字符串表示,以及__eq__()方法,用于比较两个对象是否相等等。 作用: 1. 作为基类:自定义类时,如果没有显式指定基类,则默认继承自object。 2. 提供基本方法:如__init__, __new__, __del__等特殊方法(虽然有些是由子类重写的),以及__str__, __repr__等。 3. 类型判断:可以使用isinstance()issubclass()函数来判断一个对象是否是一个类的实例,或者一个类是否是另一个类的子类,而object是所有类的祖先。 在Python3中,定义一个类时,即使不写继承,也会默认继承object,所以以下两种写法等价: class MyClass: class MyClass(object): 但在Python2中,如果不显式继承object,则创建的是旧式类(old-style class),而显式继承object则是新式类(new-style class)。新式类支持更多特性(如描述符、属性、super函数等)。现在Python2已经不再维护,所以通常我们使用Python3,默认都是新式类。 另外,object类型本身也可以用来创建实例,但这样的实例没有太多有用的属性方法。例如: obj = object() print(obj) # 输出类似于:<object object at 0x...> 在Python中,所有的数据类型都是类,而类的实例就是对象。因此,整数、字符串、列表、函数、模块等等,都是对象。 引用站内内容[^1]中提到:对象,就是你面对的所有东西都是对象,所有的数据类型,就是一种对象。英文单词是object,直接的汉语意思是物体。在编程中,面向对象也可以说成“面向东西”。这很好地解释了object的普遍性。 因此,总结一下: - object是Python中所有类的基类。 - 在Python3中,所有类都是新式类,即都直接或间接继承自object。 - object类型提供了所有对象共有的基本方法。 下面我们用一个简单的例子来说明:</think>在Python中,`object`是所有类的**基类**(根类),它是Python面向对象编程的核心基础。以下是关键解析: ### 一、本质与作用 1. **终极基类** - 所有内置类型(如`int`, `str`, `list`)用户自定义类都直接或间接继承自`object` - 验证示例: ```python issubclass(int, object) # 返回 True issubclass(str, object) # 返回 True ``` 2. **提供基础能力** 定义所有对象共有的默认行为: ```python class Person: pass # 隐式继承 object p = Person() # object 提供的基础方法 print(p.__str__()) # 默认对象字符串表示:<__main__.Person object at 0x...> print(p.__hash__()) # 默认哈希值(内存地址相关) ``` 3. **类型系统的基石** - `isinstance(任何对象, object)` 恒为 `True` - `type(object)` 返回 `<class 'type'>`,体现元类机制 ### 二、核心应用场景 1. **自定义类的隐式继承** Python 3中所有类自动继承`object`(Python 2需显式声明): ```python class MyClass: # 等价于 class MyClass(object): pass ``` 2. **特殊方法重写** 通过重写`object`的方法实现定制行为: ```python class Vector: def __init__(self, x, y): self.x = x self.y = y # 重写 object 的 __eq__ 方法 def __eq__(self, other): return self.x == other.x and self.y == other.y ``` 3. **类型检查与继承控制** ```python def log(obj: object) -> None: # 接受任何对象 if hasattr(obj, '__str__'): print(obj.__str__()) ``` ### 三、设计哲学体现 Python将一切视为对象(Everything is object)[^1]: - 函数是对象(`isinstance(print, object) == True`) - 模块是对象(`import math; isinstance(math, object) == True`) - 类本身也是对象(`isinstance(int, object) == True`) 这种设计实现了: - 统一的类型系统 - 动态属性管理(通过`__dict__`) - 反射能力(`getattr()/hasattr()`) > 引用[1]精辟总结:"所有数据类型就是一种对象",这正是Python面向对象思想的根基[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值