Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the data object NSNumber in ios

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use data objects NSNumber in ios". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use data objects NSNumber in ios".

1 Number Objects

Integers, floats, and longs are basic data types in object-c, not data objects.

Why do you need to use data objects: if you want to store data in NSArray, NSArray cannot store basic data types, so you need to put basic data types

Convert it to a data object and save it to NSArray. So that's what data objects are for.

1.1 create an integer data object

NSNumber * intNumber

NSInteger myInt

IntNumber = [NSNumber numberWithInteger: 100]

You can also use this initialization: intNumber = @ 100

/ / convert data objects to basic data types

MyInt = [intNumber integerValue]

NSLog (@ "% li", (long) myInt)

Note that the following access is incorrect:

NSLog (@ "% li", (long) [myNumber integerValue])

2 comparison of data objects

IsEqualToNumber: compare whether two data objects are equal

Compare: compare the size of 2 data objects

A compare:B

NSOrderedAscending: AB

3 the data object is immutable

NSNumber * myNumber = [[NSNumber alloc] initWithInt: 50]

...

[myNumber initWithInt: 1000]

Such code will make the program hang up, and the data object can only be initialized once.

4 numberWithInt: VS numberWithInteger:

Using the numberWithInt: method, you can only use intValue to restore its value to the base type, which can be printed using% I

Using the numberWithInteger: method, you can only use integerValue to restore its value to the basic type, and you can print its value with% li

5 the applications of api are as follows:

NumberWithChar: initWithChar: charValue

NumberWithUnsignedChar: initWithUnsignedChar: unsignedCharValue

NumberWithShort: initWithShort: shortValue

NumberWithUnsignedShort: initWithUnsignedShort: unsignedShortValue

NumberWithInteger: initWithInteger: integerValue

NumberWithUnsignedInteger: initWithUnsignedInteger: unsignedIntegerValue

NumberWithInt: initWithInt: intValue

NumberWithUnsignedInt: initWithUnsignedInt: unsignedIntValue

NumberWithLong: initWithLong: longValue

NumberWithUnsignedLong: initWithUnsignedLong: unsignedLongValue

NumberWithLongLong: initWithLongLong: longlongValue

NumberWithUnsignedLongLong: initWithUnsignedLongLong: unsignedLongLongValue

NumberWithFloat: initWithFloat: floatValue

NumberWithDouble: initWithDouble: doubleValue

NumberWithBool: initWithBool: boolValue

These are all the contents of the article "how to use data objects NSNumber in ios". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report