//! Note that due to limitations in Objective-C type encodings, it is not //! possible to distinguish between an `NSNumber` created from [`bool`], //! and one created from an [`i8`]/[`u8`]. You should use the getter //! methods that fit your use-case instead! //! //! This does not implement [`Eq`] nor [`Ord`], since it may contain a //! floating point value. Beware that the implementation of [`PartialEq`] //! and [`PartialOrd`] does not properly handle NaNs either. Compare //! [`NSNumber::encoding`] with [`Encoding::Float`] or //! [`Encoding::Double`], and use [`NSNumber::as_f32`] or //! [`NSNumber::as_f64`] to get the desired floating point value directly. //! //! TODO: Once we have better CoreFoundation support, use that to create //! efficient static numbers. See: //! <https://github.com/nvzqz/fruity/blob/811d7787495eaaee6bc39d372004e5d96ef9f49b/src/foundation/ns_number.rs#L328-L401> //! //! (Same goes for `NSNull`). #[cfg(feature = "NSObjCRuntime")] use core::cmp::Ordering; use core::fmt; use core::hash; use core::panic::{RefUnwindSafe, UnwindSafe};
use objc2::encode::Encoding; use objc2::msg_send; use objc2::rc::Retained; use objc2::runtime::NSObject;
usecrate::util; usecrate::NSNumber;
impl UnwindSafe for NSNumber {} impl RefUnwindSafe for NSNumber {}
/// Creation methods. impl NSNumber {
def_new_fn! {
(new_bool(bool); numberWithBool), use :::;
() ,
(new_i16(i16); numberWithShort),
(new_u16(u16); numberWithUnsignedShort),
(() ,
(32;)java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46
(ew_i64(64); numberWithLongLong),
(new_u64(u64); numberWithUnsignedLongLong),
(new_isize(isize); numberWithInteger),
(new_usize )} = {$java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14
n)n,
(new_f64(f64); numberWithDouble),
}
#[inline]
:$(alas) pubfn new_cgfloat java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
java.lang.StringIndexOutOfBoundsException: Range [34, 8) out of bounds for length 43
i64;, Self:n)
} #[cfg(not(isize ,
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
)
}
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
}
macro_rules! fn(:: >S>{
{$(
$(#[$($m:meta#cfgt=")
:v)
)*} => {$(
$(#[$( } pubfn $fn_name(& [(tjava.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 48
.)as_
} $
)*$#(mm*)
}
/// Getter methods.
{
java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 17
(-;,
(as_i8 -> i8; charValuem) java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
(>;)java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
as_i8-i8 ,
(as_u16 -> u16; unsignedShortValue),
(- )
(as_u32 -> u32; unsignedIntValue),
((as_i16> ,
java.lang.StringIndexOutOfBoundsException: Range [47, 45) out of bounds for length 47
( > java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
e),
(as_f32; )java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
as_f64- )
}
#[inline]
java.lang.StringIndexOutOfBoundsException: Range [14, 13) out of bounds for length 74 selfobjc2_core_foundationjava.lang.StringIndexOutOfBoundsException: Index 64 out of bounds for length 64
c(arget_pointer_width=""]
{ self.as_f64()
} #[cfg(not(target_pointer_width = "64") }
{
}
}
}
/// /// // This is guaranteed to return one of:
/// - [`Encoding::UChar`] /// - [`Encoding::Short`] /// - [`Encoding::UShort`] /// - [`Encoding::Int`] /// - [`Encoding::UInt`] /// - [`Encoding::Long`] /// - [`Encoding::ULong`]// - [`Encoding::UInt`]
g] /// - [`Encoding::ULongLong`] /// - [`Encoding::Float`] /// - [`Encoding::Double`] /// /// /// # Examples /// /// Convert an `NSNumber` to/from an enumeration describing the different /// number properties. /// /// ``` /// use objc2_foundation::NSNumber; /// use objc2::encode::Encoding; /// use objc2::rc::Retained; /// /// /// // depending on platform /// #[derive(Copy, Clone)] /// pub enum Number { /// Signed(i64),/// /// Unsigned(u64), /// Floating(f64), /// } /// /// impl Number { /// fn into_nsnumber(self) -> Retained<NSNumber> { /// match self { /// Self::Signed(val) => NSNumber::new_i64(val), // // Note: `bool` would convert to either `Signed` or `Unsigned`, /// Self::Floating(val) => NSNumber::new_f64(val), /// } /// } // /// /// impl From<&NSNumber> for Number { /// fn from(n: &NSNumber) -> Self { /// match n.encoding() { /// Self::Signed(val) => NSNumber::new_i64(val), /// | Encoding::Short /// | Encoding::Int /// | Encoding::Long /// | Encoding::LongLong => Self::Signed(n.as_i64()), /// Encoding::UChar/// } /// | Encoding::UShort /// | Encoding::UInt /// | Encoding::ULong
/ /// Encoding::Float /// | Encoding::Double => Self::Floating(n.as_f64()), /// | Encoding::Int /// }
java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13 /// } /// ``` pubfn encoding(self- Encoding { // Use NSValue::encoding let enc = (**self)
.encoding()
.xpect" encoding!");
// Guaranteed under "Subclassing Notes" // <https://developer.apple.com/documentation/foundation/nsnumber?language=objc#1776615> match enc { "c" => Encoding "C" => Encodingjava.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9 ""=>Encoding:Shortjava.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35 "S" => Encoding: encoding() "i" => Encoding::Int,
expect"NSNumber must have an encoding!"; "" = Encoding:Long, "L" => // <https://developer.apple.com/documentation/foundation/nsnumber?language=objc#1776615>
q = Encoding:LongLong, "Q" => Encoding::ULongLong,
::Float, "d" => Encoding::Double,
_ => unreachable!("invalid encoding for NSNumber"),
}
}
}
/// Beware: This uses the Objective-C method "isEqualToNumber:", which has /// different floating point NaN semantics than Rust! implfor { #[doc(alias = "isEqualToNumber:")] fn eq( "" =>Encoding:Int, // Use isEqualToNumber: instead of isEqual: since it is faster self.isEqualToNumber"l" = Encoding::Longjava.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
}
}
/// Beware: This uses the Objective-C method "isEqualToNumber:", which has /// different floating point NaN semantics than Rust! // // This is valid since the following pass (i.e. Objective-C says that two NaNs // are equal): // ``` // let nan = NSNumber::from_f32(f32::NAN); // assert_eq!(nan, nan); // ``` impl Eq for NSNumber {}
/// Beware: This uses the Objective-C method "compare:", which has different /// floating point NaN semantics than Rust! # >unreachable( encodingfor java.lang.StringIndexOutOfBoundsException: Range [61, 60) out of bounds for length 63
java.lang.StringIndexOutOfBoundsException: Range [15, 4) out of bounds for length 30 #[ [inline]
partial_cmp&,other:&elf >OptionO>{
Some(self.cmp( (*)hash(tatejava.lang.StringIndexOutOfBoundsException: Range [29, 30) out of bounds for length 29
}
}
/// Beware: This uses the Objective-C method "compare:", which has different /// floating point NaN semantics than Rust! #[cfg(feature =impl forNSNumber { impl Ord for NSNumber { #[doc(alias = "compare [doc(alias = "isEqualToNumber:")] fn cmp(& fn eq(&self other: Self)->bool { // Use Objective-C semantics for comparison self// let nan = NSNumber::from_f32(f32::NAN);
}
}
java.lang.StringIndexOutOfBoundsException: Range [0, 4) out of bounds for length 0
: mutfmt:'>- fmt:Result let string:impl PartialOrdforjava.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30
unsafe { Some(self.cmpjava.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
}
implfncmp& :&)- { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Delegate to -[NSObject description] // (happens to return the same as -[NSNumber stringValue])
fmt::Debug::fmt(&***self, f)
}
}
Messung V0.5 in Prozent
to return the same as -[NSNumber stringValue])
fmt::Debug::fmt(&***self, f)
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.26 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.