Skip to content

Commit 833f850

Browse files
madsmtmrnapier
andcommitted
Basic version of NSError
Inspiration from SSheldon/rust-objc-foundation#16 Co-authored-by: Rob Napier <[email protected]>
1 parent e5fda1a commit 833f850

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

objc2-foundation/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1717
* Added `NSThread` object.
1818
* Added `is_multi_threaded` and `is_main_thread` helper functions.
1919
* Added `NSProcessInfo` object.
20+
* Added `NSError` object.
2021

2122
### Changed
2223
* **BREAKING**: Removed the following helper traits in favor of inherent

objc2-foundation/src/error.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use core::ptr::NonNull;
2+
3+
use objc2::ffi::NSInteger;
4+
use objc2::msg_send;
5+
use objc2::rc::{Id, Shared};
6+
7+
use crate::{NSDictionary, NSObject, NSString};
8+
9+
pub type NSErrorUserInfoKey = NSString;
10+
11+
#[allow(non_snake_case)]
12+
pub fn NSLocalizedDescriptionKey() -> Id<NSString, Shared> {
13+
NSString::from_str(&"NSLocalizedDescription")
14+
}
15+
16+
object!(
17+
unsafe pub struct NSError: NSObject;
18+
);
19+
20+
impl NSError {
21+
pub fn new<T>(
22+
code: NSInteger,
23+
domain: Id<NSString, Shared>,
24+
user_info: Id<NSDictionary<NSErrorUserInfoKey, T>, Shared>,
25+
) -> Id<Self, Shared> {
26+
let cls = Self::class();
27+
unsafe {
28+
let obj: *mut Self = msg_send![cls, alloc];
29+
let obj: *mut Self =
30+
msg_send![obj, initWithDomain: &*domain, code: code, userInfo: &*user_info];
31+
Id::new(NonNull::new_unchecked(obj))
32+
}
33+
}
34+
}

objc2-foundation/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub use self::copying::{NSCopying, NSMutableCopying};
2727
pub use self::data::{NSData, NSMutableData};
2828
pub use self::dictionary::NSDictionary;
2929
pub use self::enumerator::{NSEnumerator, NSFastEnumeration, NSFastEnumerator};
30+
pub use self::error::{NSError, NSErrorUserInfoKey, NSLocalizedDescriptionKey};
3031
pub use self::object::NSObject;
3132
pub use self::process_info::NSProcessInfo;
3233
pub use self::range::NSRange;
@@ -51,6 +52,7 @@ mod copying;
5152
mod data;
5253
mod dictionary;
5354
mod enumerator;
55+
mod error;
5456
mod object;
5557
mod process_info;
5658
mod range;

0 commit comments

Comments
 (0)