再来IOS开发小知识点总结

一、获取当前日期和星期

// get the current date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *now = [[NSDate alloc] init];
 
// get the weekday of the current date
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* components = [cal components:NSWeekdayCalendarUnit fromDate:now];
NSInteger weekday = [components weekday]; // 1 = Sunday, 2 = Monday, etc.
						

二、.[[UIDevice currentDevice] orientation] 获取设备状态

[[UIDevice currentDevice] orientation] 通过这个获取设备状态是有以下七个状态:
UIDeviceOrientationUnknown                 
UIDeviceOrientationPortrait             //Device oriented vertically, home button one the button
UIDeviceOrientationPortraitUpsideDown   //Device oriented vertically, home button one the top
UIDeviceOrientationLandscapeLeft     //Device oriented horizontally,home button on the right
UIDeviceOrientationLandscapeRight         //Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp                     //Device oriented flat, face up
UIDeviceOrientationFaceDown                 //Device oriented flat, face down
在有第一个和最后两个状态的情况下,如果是在状态发生变化的时候去做一些操作,那么是没问题的,因为操作的时候只捕获四个旋转状态即可。但是如果要根据在做操作的时候根据状态去做某些事情就会有问题,因为非常有可能取到那三个非旋转状态。解决方案:记住上一个旋转状态,当发现获取的状态不是四个旋转状态的时候,使用上一个正常旋转状态。

示例代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
 
   [window addSubview:crateViewController.view];
     
   [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
     
   [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(orientationChanged:)
      name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
  UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
  if (UIDeviceOrientationIsPortrait(orientation) || UIDeviceOrientationIsLandscape(orientation))
  {
    if (!currentOrientation)
    {
      currentOrientation = orientation;
      [self prepareScreen];
    }
    currentOrientation = orientation;
  }
  [[UIApplication sharedApplication] setStatusBarOrientation:currentOrientation animated:YES];
}

三、多线程之NSInvocationOperation

多线程主要是为了防止主线程堵塞,增加运行效率等等的最佳方法,而在原始的多线程中可能会造成线程锁死等现象,在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。首先简单介绍一下Apple提供给我们的API。

多线程API介绍
NSOperation基类,用来自定义operation object。继承NSOperation 可以完全控制operation object 的实现,包括修改操作执行和状态报告的方式等。
NSInvocationOperation可以直接使用的类,基于应用的一个对象和selector来创建operation  object。如果你已经有现有的方法来执行需要的任务,就可以使用这个类。
NSBlockOperatin可以直接使用的类,用来并发的执行一个或者多个block对象,operation object 使用“组”的语义来执行多个block对象,所有相关的block都在执行完成之后,operation object才算完成。
下面是一段使用 NSInvocationOperation的示例代码:

@implementation MyCustomClass   
- (void)launchTaskWithData:(id)data
{
    //创建一个NSInvocationOperation对象,并初始化到方法
    //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method)
    //在这里,object后的值是想传递给前面方法的数据
    NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self
                    selector:@selector(myTaskMethod:) object:data];   //下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行)
    // 更多的时候是由我们自己建立“操作”队列
    [[MyAppDelegate sharedOperationQueue] addOperation:theOp];
}  
 // 这个是真正运行在另外一个线程的“方法”
- (void)myTaskMethod:(id)data
{
    // Perform the task.
}  
 @end
一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:
@interface MyViewController : UIViewController {   
	NSOperationQueue *operationQueue;
    //在头文件中声明该队列
}
@end   
@implementation MyViewController   
- (id)init
{
    self = [super init];
    if (self) {
        operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列
        [operationQueue setMaxConcurrentOperationCount:1];
        //在这里限定了该队列只同时运行一个线程
        //这个队列已经可以使用了
    }
    return self;
}   
 @end

四、如何在应用程序中发送邮件

3.0以前使用mailto URL,但是会退出当前应用程序

3.0后Apple提供了MessageUI framework可以在我们的应用程序内实现邮件发送功能,代码示范参见:

https://developer.apple.com/iphone/library/samplecode/MailComposer/index.html

可以添加附件和以HTML格式发送邮件。

 

如果想要在邮件中添加URL如:http://ditu.at/tinyurl
可以如下编码:
// Fill out the email body text
NSString *emailBody = [[NSString alloc]init];
NSArray *arSubviews = [shareView subviews];
UITextField *tvMessage = [arSubviews objectAtIndex:4];
emailBody = [NSString stringWithFormat:@"%@,%@ <a href = '%@'>%@</a>", tvMessage.text, NSLocalizedString(@"FOR_DETAILS",@""), 
[self.tinyURLList objectAtIndex:0],[self.tinyURLList objectAtIndex:0]];
如果想在邮件中提供你的App Store应用程序链接,可如下编码:

NSString *pageLink = @"http://mugunthkumar.com/yourapp"; 
NSString *iTunesLink = @"http://link-to-yourapp"; 
NSString *emailBody =
[NSString stringWithFormat:@"%@\n\n<h3>Sent from <a href = '%@'>your app</a> on iPhone. <a href = '%@'>Download</a> yours from AppStore now!</h3>", @"test url", pageLink, iTunesLink];

最后一定记住使用html格式:

[mailPicker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:mailPicker animated:YES];

五、深刻理解一下IOS  SDK  和  Objective-c

iPhone OS

iPhone OS 由4个主要部分组成。下面简单地罗列一下它们的功能。

Cocoa Touch
  • 窗口和视图
  • 事件管理
  • 用户接口
  • 加速传感器
  • 照相机
Media
  • Core Graphics(2维图形接口)
  • Core Animation(动画)
  • OpenGL
  • Core Audio(声音)
  • OpenAL
  • Media Player(MPEG4,MP3)
Core Services
  • Address Book
  • Core Foundation
  • Core Location
  • CFNetwork(http,https,ftp,SSL,TLS)
  • 网络安全
  • SQLite(SQL数据库)
  • XML
Core OS
  • 多线程
  • 网络应用(BSD套接字)
  • 文件系统
  • Bonjour(利用无线网络连接其他机器)

iPhone SDK

iPhone SDK 中主要包含下列4个工具。

  • Xcode - 项目管理、代码编辑、编译、调试(IDE)
  • Interface Builder - GUI 设计
  • iPhone Simulator - 模拟器
  • Instrument - 性能测试、调整

实际开发的过程中,基本上是在使用 Xcode 与 Interface Builder 来进行的。调试则是使用模拟器或者实际设备。要注意的是在PC上模拟程序,由于PC的主频,性能高于实际设备,所以不能只在模拟器上调试。除此之外,一些类,功能在模拟器上也是不能使用的,比如 NSDateCalendar 类,或者是照相机功能。

Objective-C 2.0

内存管理

虽然 Objective-C 2.0 已经支持了垃圾收集了,但是 iPhone OS 中却不能使用它。所以我们需要自己来管理内存。Objective-C 的内存管理方式与使用引用计数的方式,就是说对象有一个计数器,引用对象一次,计数器加一,当计数器为0的时候,该对象的内存被释放。

创建对象实例的时候(init,alloc)应用计数加一,执行过程中,别的对象如果需要该对象,需要用(retain)来引用它,这时,该对象的应用计数器加一。不需要对象的时候用(release)来释放,这时引用计数器减一,当计数器为0的时候,释放该对象内存。

  • init,alloc - 计数器 +1
  • retain - 计数器 +1
  • release - 计数器 -1

另外如果不使用 retain,release,可以使用(autorelease)来自动释放对象。

容器

Objective-C 中的容器主要有以下3种:

  • 数组
  • 字典
  • Set

向容器中添加的内容不能直接用 int 或 float,需要通过 NSNumber 等封装类来实现。Objective-C 2.0 开始可以使用迭代子(Enumerator),来顺序访问容器中的元素。

Notification

Notification是消息通知的功能。具体使用 NSNotificationCenter 类。将需要接受通知的对象,方法,事件注册到该类上。

归档(Archive)

归档是指将对象的内存布局原样地保存到文件系统上。同样对应的由文件中的数据生成对象叫做UnAchive。在 iPhone SDK 中使用 NSKeyedArchiver 和 NSKeyedUnarchiver 类来实现。

一般在程序结束的时候,保存当前的状态,再次启动的时候UnAchive一下,就又回到了刚才退出时的状态。下面是一个例子:<示例代码来自网络>

// MyKeyedArchiver.h
#import <Cocoa/Cocoa.h>

@interface NSKeyedArchiver (MyKeyedArchiver)

- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address;

@end

#import "MyKeyedArchiver.h"

@implementation NSKeyedArchiver (MyKeyedArchiver)

- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address
{
    NSMutableData *datas = [NSMutableData data];
    NSArchiver *arch = [[NSArchiver alloc] initForWritingWithMutableData:datas];
    [arch encodeValueOfObjCType:valueType
                             at:address];
    [self encodeObject:[NSData dataWithData:datas]];
    [arch release];
}

@end

// MyKeyedUnarchiver.h
#import <Cocoa/Cocoa.h>

@interface NSKeyedUnarchiver (MyKeyedUnarchiver)

- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data;

@end

#import "MyKeyedUnarchiver.h"

@implementation NSKeyedUnarchiver (MyKeyedUnarchiver)

- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data
{
    NSData *datas = [self decodeObject];
    NSUnarchiver *unarch = [[NSUnarchiver alloc] initForReadingWithData:datas];
    [unarch decodeValueOfObjCType:valueType
                               at:data];
    [unarch release];
}

@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值