当前位置:  编程技术>移动开发
本页文章导读:
    ▪Objective-c中Foundation中的几个惯用类        Objective-c中Foundation中的几个常用类  使用XCode的提示功能,只需要记住类的名字及常用的函数就好了,其他的函数可以根据提示来查看,按ESC键可以查看该类的函数          Foundtion框架    .........
    ▪ Objective-c中的占位符,打印BOOL门类数据        Objective-c中的占位符,打印BOOL类型数据 常用的一些占位符: %@:字符串占位符 %d:整型 %ld:长整型 %f:浮点型 %c:char类型 %%:%的占位符 尽管有那么多的占位符,但是好像没有发现BOOL型的数.........
    ▪ MediaPlayer报错prepareAsync called in state 八       MediaPlayer报错prepareAsync called in state 8错误总结: MediaPlayer报错prepareAsync called in state 8 原因: 在进入Activity后在Oncreate()方法中: mMediaPlayer=MediaPlayer.create(MainActivity.this, R.raw.big); 为此mMediaPlayer设置.........

[1]Objective-c中Foundation中的几个惯用类
    来源: 互联网  发布时间: 2014-02-18
Objective-c中Foundation中的几个常用类

 使用XCode的提示功能,只需要记住类的名字及常用的函数就好了,其他的函数可以根据提示来查看,按ESC键可以查看该类的函数        

 Foundtion框架 

         Cocoa程序的编写主要要用到两个框架,Foundation和ApplicationKit(UIKit),其中Foundation框架主要定义了一些基础类,而ApplicationKit主要定义了一些用于Mac开发的几面基础类,而IOS的界面开发主要是用UIKit。Foundation框架中的所有类都继承自NSObject,这就是所谓的上帝吧。Foundation主要提供了与图形用户界面没有直接关系的功能的一些类,比如:字符串、数值、容器集合等等相关的类。

         1、有关数字对象的处理

            把数字包装成数字对象

    int age = 24;
    BOOL isMarry = NO;
    float pi = 3.14f;
    //使用类方法,其他的基本数据类型和如下两种方式相同
    NSNumber *myAge = [NSNumber numberWithInt:age];
    NSNumber *aboutMarry = [NSNumber numberWithBool:isMarry];
    //使用初始化方法
    NSNumber *aboutPi = [[NSNumber alloc]initWithFloat:pi];

            把数字对象再转换成基本数据类型

    age = [myAge intValue];
    isMarry = [aboutMarry boolValue];
    pi = [aboutPi floatValue];

         2、字符串的常见应用

            由于oc是基于c的,为了区别起见,oc中的字符串必须以@开头,@后引号内的类容为字符串本身内容

            NSString对象一旦创建就不能再修改,如果想创建一个可以修改的字符串对象,则使用NSMutableString,这里的NSS他ring好比java中的String类,而NSMutableString类好比java中StringBuffer

        
        //创建一个字符串
        //方法一
        NSString *name = @"Jim Green";
        //方法二
        //创建一个空字符串
        NSString *name1 = [[NSString alloc]init];//实例方法
        
        //创建非空字符串
        NSString *name3 = [[NSString alloc]initWithString:@"Jim Green"];
               
        //有关创建格式化符字符串
        NSString *myself = [[NSString alloc]initWithFormat:@"我是%@,今年%d岁,知道PI的值是%f",name3,age,pi];

有关字符串的比较:

 //以下打印结果为相等
       NSString *str1 = @"niao";
        NSString *str2 = @"niao";
        
        if (str1 == str2) {
            
            NSLog(@"相等");
        }else
            NSLog(@"不相等");
        
        if ([str1 isEqualToString: str2]) {
            
            NSLog(@"相等");
        }else
            NSLog(@"不相等");
        
       //此处的打印结果也相等(在常量区创建)
        NSString *str3 = [[NSString alloc]initWithString:@"ge"];
        NSString *str4 = [[NSString alloc]initWithString:@"ge"];
        
        if (str3 == str4) {
            
            NSLog(@"相等");
        }else
            NSLog(@"不相等");
        
        //此处打印也相等
        if ([str3 isEqualToString: str4]) {
            
            NSLog(@"相等");
        }else
            NSLog(@"不相等");
        
        //********************************************************分界线
        //在堆区创建
        NSString *str5 = [NSString stringWithFormat:@"ni%d",5];
        NSString *str6 = [NSString stringWithFormat:@"ni%d",5];
        
        //此处打印不相等
        if (str5 == str6) {
         NSLog(@"5相等6");
         }else
             NSLog(@"5不相等6");
         //此处打印也相等
        if ([str5 isEqualToString: str6]) {
            NSLog(@"5相等6");
         }else
            NSLog(@"5不相等6");

         分界线以上的无论是==还是isEqualToString都是相等,分界线以下的==打印不相等,isEqualToString打印的是相等,这里用isEqualToString比较相等很容易理解,关于用等号比较

         那是因为用==比较的是对象指针的地址,而不是对象本身

         而分界线以上的对象都是在敞亮区创建的,所以用==比较是相等的

         而分解先以下的是在堆区创建的,所以用==比较是不相等的

         str5与str6是在堆区创建了两个对象,所以对象的地址是不同的所以用==比较不同,而对象的内容是相同的,所以isEqualToString比较相同。


 比较字符串的大小

NSString *str7 = @"a";
        NSString *str8 = @"b";
        NSString * str9 = @"A";
        
        NSLog(@"比较结果为:%@",[str7 compare:str8]?@"YES":@"NO");//比较结果为:YES
        //忽略大小写
        NSLog(@"忽略大小写1,%ld",[str7 caseInsensitiveCompare:str8]);//忽略大小写1,-1
        NSLog(@"忽略大小写2,%ld",[str9 caseInsensitiveCompare:str7]);//忽略大小写2,0




    
[2] Objective-c中的占位符,打印BOOL门类数据
    来源: 互联网  发布时间: 2014-02-18
Objective-c中的占位符,打印BOOL类型数据

常用的一些占位符:

%@:字符串占位符

%d:整型

%ld:长整型

%f:浮点型

%c:char类型

%%:%的占位符

尽管有那么多的占位符,但是好像没有发现BOOL型的数据的占位符,这也是比较纠结的地方,看了一下别人是怎么解决这个问题的

 BOOL studyBool = YES;
        NSLog(@"打印BOOL型数据%@",studyBool?@"YES":@"NO");//打印BOOL型数据YES
        NSLog(@"打印BOOL型数据%d",studyBool);//打印BOOL型数据1
        
        BOOL alsoBool = NO;
        NSLog(@"打印BOOL型数据%@",alsoBool?@"YES":@"NO");//打印BOOL型数据NO
        NSLog(@"打印BOOL型数据%d",alsoBool);//打印BOOL型数据0


详细介绍:**********************************************************

%@:             Objective-C对象,印有字符串返回descriptionWithLocale:如果于的话,或描述相反.CFTypeRef工作对象,返回的结果的CFCopyDescription功能.(这个翻译有问题建议按照自己的理解方式理解)。

%%:             为'%'字符;

%d,%D,%i:   为32位整型数(int);

%u,%U:        为32位无符号整型数(unsigned int);

%hi:   为有符号的16位整型数(short);

%hu:  为无符号的16位整型数(unsigned shord);

%qi:   为有符号的64位整型数(long long);

%qu:  为无符号的64位整型数(unsigned long long);

%x:    为32位的无符号整型数(unsigned int),打印使用数字0-9的十六进制,小写a-f;

%X:    为32位的无符号整型数(unsigned int),打印使用数字0-9的十六进制,大写A-F;

%qx:   为无符号64位整数(unsigned long long),打印使用数字0-9的十六进制,小写a-f;

%qX:   为无符号64位整数(unsigned long long),打印使用数字0-9的十六进制,大写A-F;

%o,%O:   为32位的无符号整数(unsigned int),打印八进制数;

%f:      为64位的浮点数(double);

%e:      为64位的浮点数(double),打印使用小写字母e,科学计数法介绍了指数的增大而减小;

%E:      为64位的浮点数(double),打印科学符号使用一个大写E介绍指数的增大而减小;

%g:      为64位的浮点数(double),用%e的方式打印指数,如果指数小于4或者大于等于精度,那么%f的风格就会有不同体现;

%G:      为64位的浮点数(double),用%E的方式打印指数,如果指数小于4或者大于等于精度,那么%f的风格就会有不同体现;

%c:       为8位的无符号字符%c(unsigned char),通过打印NSLog()将其作为一个ASCII字符,或者,不是一个ASCII字符,八进制格式\ddd或统一标准的字符编码的十六进制格式\udddd,在这里d是一个数字;

%C:       为16位Unicode字符%C(unichar),通过打印NSLog()将其作为一个ASCII字符,或者,不是一个ASCII字符,八进制格式\ddd或统一标准的字符编码的十六进制格式\\udddd,在这里d是一个数字;

%s:       对于无符号字符数组空终止,%s系统中解释其输入编码,而不是别的,如utf-8;

%S:       空终止一系列的16位Unicode字符;

%p:       空指针(无效*),打印十六进制的数字0-9和小写a-f,前缀为0x;

%L:       在明确规定的长度下,进行修正,下面的一批数据a,A,e,E,f,F,g,G应用于双精度长整型的参数;

%a:       为64位的浮点数(double),按照科学计数法打印采用0x和一个十六进制数字前使用小写小数点p来介绍指数的增大而减小;

%A:       为64位的浮点数(double),按照科学计数法打印采用0X和一个十六进制数字前使用大写字母小数点P界扫指数的增大而减小;

%F:       为64位的浮点数(double),按照十进制表示法进行打印;

%z:       修改说明在%z长度以下d,i,o,u,x,X适用于某一指定类型的转换或者适用于一定尺寸的整数类型的参数;

%t:       修改说明在%t长度以下d,i,o,u,x,X适用于某一指定类型或一定尺寸的整数类型的转换的参数;

%j:       修改说明在%j长度以下d,i,o,u,x,X适用于某一指定类型或一定尺寸的整数类型的转换的参数。


英文文档

格式定义
The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification; the specifiers are summarized in Table 1. Note that you can also use the “n$” positional specifiers such as %1$@ %2$s. For more details, see the IEEE printf specification. You can also use these format specifiers with the NSLog function.

Table 1 Format specifiers supported by the NSString formatting methods and CFString formatting functions 定义 说明 %@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function. %% ‘%’ character %d, %D, %i Signed 32-bit integer (int) %u, %U Unsigned 32-bit integer (unsigned int) %hi Signed 16-bit integer (short) %hu Unsigned 16-bit integer (unsigned short) %qi Signed 64-bit integer (long long) %qu Unsigned 64-bit integer (unsigned long long) %x Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f %X Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F %qx Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and lowercase a–f %qX Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and uppercase A–F %o, %O Unsigned 32-bit integer (unsigned int), printed in octal %f 64-bit floating-point number (double) %e 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent %E 64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent %g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise %G 64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise %c 8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit %C 16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit %s Null-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8. %S Null-terminated array of 16-bit Unicode characters %p Void pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x %L Length modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument %a 64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent %A 64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent %F 64-bit floating-point number (double), printed in decimal notation %z Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument %t Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument %j Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a intmax_t or uintmax_t argument

平台依赖
Mac OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 2. Note that in some cases you may have to cast the value.

Table 2 Format specifiers for data types 类型 定义 建议 NSInteger %ld or %lx Cast the value to long NSUInteger %lu or %lx Cast the value to unsigned long CGFloat %f or %g %f works for floats and doubles when formatting; but see below warning when scanning CFIndex %ld or %lx The same as NSInteger pointer %p %p adds 0x to the beginning of the output. If you don’t want that, use %lx and cast to long. long long %lld or %llx long long is 64-bit on both 32- and 64-bit platforms unsigned long long %llu or %llx unsigned long long is 64-bit on both 32- and 64-bit platforms

The following example illustrates the use of %ld to format an NSInteger and the use of a cast.

1
2
NSInteger i = 42;
printf("%ld\n", (long)i);

In addition to the considerations mentioned in Table 2, there is one extra case with scanning: you must distinguish the types for float and double. You should use %f for float, %lf for double. If you need to use scanf (or a variant thereof) with CGFloat, switch to double instead, and copy the double to CGFloat.

1
2
3
4
CGFloat imageWidth;
double tmp;
sscanf (str, "%lf", &tmp);
imageWidth = tmp;

It is important to remember that %lf does not represent CGFloat correctly on either 32- or 64-bit platforms. This is unlike %ld, which works for long in all cases.



    
[3] MediaPlayer报错prepareAsync called in state 八
    来源: 互联网  发布时间: 2014-02-18
MediaPlayer报错prepareAsync called in state 8

错误总结:
MediaPlayer报错prepareAsync called in state 8


原因:
在进入Activity后在Oncreate()方法中:
mMediaPlayer=MediaPlayer.create(MainActivity.this, R.raw.big);
为此mMediaPlayer设置了要播放的资源
但是在点击stop按钮以后,执行了mMediaPlayer.release();
这样的话就释放了资源.所以再次点击播放的时候报错

解决办法:
在每次点击play的时候,都执行MediaPlayer.create()

注意:
可以在每次点击暂停的时候保存现在播放的位置即:
position=mMediaPlayer.getCurrentPosition();
然后在每次播放的时候
mMediaPlayer.seekTo(position);
这样就会从暂停处继续播放


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3