Dark Horse programmer--foundation Framework Common Class (Nsnumber/nsvalue)

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! ------

Brief introduction

Introduced: Nsarray, Nsset, nsdictionary these OC collections can only hold OC objects and cannot hold basic data types (int, char, double ...). )。 If we want to store the basic data types in these OC collections, we will base data types (int, char, double ...). ) is packaged into OC objects, then it can be stored in the OC set. NSNumber can wrap a basic data type as an OC object.


Nsvalue: Used to store a C or OC data, you can wrap the base data type (int, double). ), you can also package the structure (Cgpoint, cgsize, CGRect).

NSNumber: Inherited from Nsvalue, which is typically used to store basic data types (int, double) in the C language. )。

Nsnumber/nsvalue1. Conversion of numeric objects to basic data types

1.int <-> Nsnumberint a = 10; NSNumber *numa = [NSNumber numberwithint:a];int Tempa = numa.intvalue;//2.double <-> nsnumberdouble b = 20.5f; NSNumber *numb = [NSNumber numberwithdouble:b];d ouble TEMPB = Numb.doublevalue;//3.char <-> Nsnumberchar ch = ' A ';// NSNumber *numch = @ (CH); NSNumber *numch = [NSNumber Numberwithchar:ch];char tempch = numch.charvalue; NSLog (@ "%c", tempch);//4.nsstring <-> Nsnumberchar *str = "string"; NSNumber *numstr = [NSNumber numberwithchar:str];char *tempstr = numstr.charvalue;//5. Compiler features/*@1 = = [NSNumber NUMBERWITHINT:1]; *@ "string" = = [NSNumber numberwithint: "string"]; *int a = 10; *@ (a) = = [NSNumber numberwithint:a]; */

2. Basic use of Digital objects

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {    @autoreleasepool {        //1. struct  <-> Nsvalue        Cgpoint a = {n};        Nsvalue *vaa = [Nsvalue valuewithpoint:a];        Cgpoint Tempa = vaa.pointvalue;                The cgpoint is packaged into OC objects, which can be deposited into the OC set        nsmutableset *set = [Nsmutableset set];        [Set ADDOBJECT:VAA];        The double type is wrapped into a NSNumber object, which can be stored in the set        [set addobject:@10.5];                [Set Enumerateobjectsusingblock:         ^ (id obj, BOOL *stop) {             NSLog (@ "%@", obj);        }];                    }    return 0;}
result output:
2015-05-10 22:47:41.018 09-nsnumber Basic use [896:38347] Nspoint: {1, 2}2015-05-10 22:47:41.019 09-nsnumber basic use [896:38,347] 10.5



Dark Horse programmer--foundation Framework Common Class (Nsnumber/nsvalue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.