docs

v0.1.2 문서를 보고 있습니다. 이 버전은 더 이상 지원하지 않습니다. 최신 릴리스: v0.1.3 →

bare::ffi::objc

이 페이지의 내용

API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.

Objective-C objects on Apple targets.

Classes and their methods are declared in extern "objc" blocks, which the compiler turns into a type per class and methods on Id: each method sends its message with objc_msgSend under the C ABI of its signature, and its selector is a static reference the runtime fixes up at load. This module holds what every program shares: Id, Sel, Class, the root class NSObject, NSString and str, NSError as Error, autorelease pools and blocks.

An Id<T> owns one retain of its object. Copies share it; the collector gives it back once no copy is left, and release_now() gives it back at once. A WeakId<T> refers to an object without keeping it.

T: IsA<C> says a T is a C: C itself, a superclass, or a protocol it conforms to. The methods of C are on every Id<T> with T: IsA<C>, and a parameter of class C takes them.

Modules

  • block Nabi closures as Objective-C blocks, laid out as the Apple block ABI says.
  • dispatch Grand Central Dispatch: run a closure on the main queue or a global one, through a Block.
  • error NSError, what a method that reports through a last NSError ** parameter throws.
  • string NSString and str.
  • subclass classes made at run time (objc_allocateClassPair): Nabi functions as an Objective-C class's methods, for delegates and the classes an API wants subclassed.

Re-exports

block::{Block, BlockRef}Source

pub use block::{Block, BlockRef}
  • Block A block Nabi owns (one _Block_copy retain, which the collector gives back), calling a closure of type F.
  • BlockRef A block Objective-C passed in (a completion handler): valid while the call that passed it runs; copy() keeps it longer.

dispatch::{Queue, dispatch_async}Source

pub use dispatch::{Queue, dispatch_async}
  • Queue A dispatch queue (dispatch_queue_t).
  • dispatch_async Run block on queue later (dispatch_async); the queue keeps the block until it ran.

error::{Error, NSError}Source

pub use error::{Error, NSError}
  • Error An NSError a method reported, thrown by throws (objc::Error).
  • NSError NSError, Foundation's error class.

string::{NSString, ns}Source

pub use string::{NSString, ns}
  • NSString NSString, Foundation's string class: a program using it links Foundation (#[link(name = "Foundation", kind = "framework")]).
  • ns NSString::from_str(s), short: ns("text").

subclass::ClassBuilderSource

pub use subclass::ClassBuilder
  • ClassBuilder A class being made: add its methods and protocols, then register.

Structs

SelSource

pub struct Sel

A selector: the name of a method, as the runtime registered it.

  • pub fn register(name: str) -> Sel§

    Source

    The selector named name ("initWithFrame:"), registered with the runtime.

  • pub fn name(self) -> str§

    Source

    Its name, as register was given it.

Trait implementations 5

ClassSource

pub struct Class

A class object.

  • pub fn lookup(name: str) -> Class | none§

    Source

    The class named name, if the program has loaded it.

  • pub fn name(self) -> str§

    Source

    Its name.

  • pub fn as_raw(self) -> CPtr<u8>§

    Source

    The class as an object pointer, the receiver of a class method.

  • pub fn new_object(self) -> Id<NSObject>§

    Source

    +[C new]: a new instance, of a class ClassBuilder made or another one Nabi has no type for.

Trait implementations 5

NSObjectSource

pub struct NSObject

NSObject, the root class: its methods are on every Id.

Trait implementations 2
  • impl Object for NSObject
  • impl IsA<NSObject> for NSObject

IdSource

pub struct Id<T>

An Objective-C object of class T (or a subclass of it), holding one retain. Copies of an Id share that retain; the collector releases it once no copy is left, and release_now() on any of them gives it back at once.

  • pub unsafe fn from_retained(p: CPtr<u8>) -> Id<T> | none§

    Source

    Take over the retain the caller owns on p (a +1 result: alloc, new, copy, init); none for nil.

    Safety: p is nil or an object of class T the caller owns once.

  • pub unsafe fn retain(p: CPtr<u8>) -> Id<T> | none§

    Source

    Retain p, an object the caller does not own (a +0 result); none for nil.

    Safety: p is nil or a live object of class T.

  • pub fn as_raw(self) -> CPtr<u8>§

    Source

    The object pointer, for a message send; panics once released.

  • pub fn retained_raw(self) -> CPtr<u8>§

    Source

    A retain of the object for a callee that takes one over (an NS_CONSUMED parameter).

  • pub fn is_released(self) -> bool§

    Source

    Whether release_now() gave the object back.

  • pub fn release_now(self)§

    Source

    Release the object now, not when the last copy goes. Every copy of this Id then reads as released, and sending to one panics. A second call does nothing.

  • pub unsafe fn cast<U: Object>(self) -> Id<U>§

    Source

    The same object (and the same retain) as class U.

    Safety: the object is a U; downcast checks it.

  • pub fn downcast<U: Object>(self) -> Id<U> | none§

    Source

    The object as a U when it is one (or a subclass of it).

  • pub fn as_object(self) -> Id<NSObject>§

    Source

    The object as an NSObject, for a parameter that takes any object.

  • pub fn upcast<U: Object>(self) -> Id<U> where T: IsA<U>§

    Source

    The object as a U, which a T is (a superclass or a protocol it conforms to): button.upcast::<NSView>().

  • pub fn description(self) -> Id<NSString>§

    Source

    -[NSObject description].

  • pub fn hash(self) -> usize§

    Source

    -[NSObject hash].

  • pub fn isEqual<U: Object>(self, other: Id<U>) -> bool§

    Source

    -[NSObject isEqual:].

  • pub fn isKindOfClass(self, c: Class) -> bool§

    Source

    -[NSObject isKindOfClass:].

  • pub fn respondsToSelector(self, s: Sel) -> bool§

    Source

    -[NSObject respondsToSelector:].

  • pub fn class(self) -> Class§

    Source

    -[NSObject class].

  • pub fn retainCount(self) -> usize§

    Source

    -[NSObject retainCount], for tests: the count Objective-C code sees, one of which this Id holds.

Trait implementations 4

WeakIdSource

pub struct WeakId<T>

A weak reference to an object: it does not keep the object alive, and reads as none once the object is deallocated. The answer to a cycle through Objective-C (a delegate that holds what holds it).

  • pub fn new(o: Id<T>) -> WeakId<T>§

    Source

    A weak reference to o's object.

  • pub fn load(self) -> Id<T> | none§

    Source

    The object, retained, while it is alive; none after.

  • pub fn release_now(self)§

    Source

    Give back the weak slot now, not when the collector does; load then returns none.

Trait implementations 2
  • impl<T> Clone for WeakId<T>
  • impl<T> Copy for WeakId<T>

Traits

ObjectSource

pub trait Object

A Nabi type standing for an Objective-C class or protocol: the marker types extern "objc" blocks declare, and NSObject, NSString, NSError.

  • fn name() -> str§

    Source

    The class or protocol name.

  • fn class() -> Class§

    Source

    The class object; a protocol has none and panics.

  • fn main_thread() -> bool§

    Source

    Its objects are released on the main thread (NSView, NSWindow and the other NSResponders).

IsASource

pub trait IsA<C>

A Self is a C: C itself, one of its superclasses, or a protocol it (or a superclass) conforms to. extern "objc" blocks declare these.

Functions

newSource

pub fn new<T: Object>() -> Id<T>

-[NSObject new] style: an object of class T made with alloc and init.

autoreleasepoolSource

pub fn autoreleasepool<R>(f: fn() -> R) -> R

Run f inside an autorelease pool: objects Objective-C code autoreleases meanwhile are released when it returns. Wrap loops that call many methods; each Id keeps its own object regardless.

availableSource

pub fn available(macos: str = "", ios: str = "") -> bool

Whether the running OS is the given version or newer: macOS macos ("13.0"), iOS ios ("16.0"); an empty one (the default) is any version of that OS. Inside if objc::available(macos: "13.0", ios: "16.0") { .. } the APIs those versions brought may be called though the deployment target is older; the compiler refuses them elsewhere.