Skip to content

Instantly share code, notes, and snippets.

@xxd
Created May 15, 2012 10:05
Show Gist options
  • Save xxd/2700534 to your computer and use it in GitHub Desktop.
Save xxd/2700534 to your computer and use it in GitHub Desktop.
Objc语法纪录:NSSet,UIAlert&switch,Fast_Enumeration,NSString,NSArray,Delegate,ternary
// AZDelegationDemo.h
// Created by Aladdin Zhang on 8/24/11.
#import
@protocol AZDelegationDemoDelegate;
@interface AZDelegationDemo : NSObject{
idAZDelegationDemoDelegate > delegate;
NSMutableArray * shoppingList;
}
@property (nonatomic,assign) idAZDelegationDemoDelegate > delegate;
@end
@interface AZDelegationDemo (informal_protocol_delegate)
- (void)buyCoffee;
@end
@protocol AZDelegationDemoDelegate NSObject >
@required
- (void)buyPork;
@optional
- (void)buyMilk;
@end
// AZDelegationDemo.m
// Created by Aladdin Zhang on 8/24/11.
#import "AZDelegationDemo.h"
@implementation AZDelegationDemo
@synthesize delegate;
- (id)init{
self = [super init];
if (self) {
shoppingList = [NSMutableArray arrayWithCapacity:20];
}
return self;
}
- (void)buyStuff{
NSLog(@"My Shopping List %@",shoppingList);
}
- (void)weeklyShopping{
if (self.delegate&&[self.delegate conformsToProtocol:@protocol(AZDelegationDemoDelegate)]) {
[self.delegate performSelector:@selector(buyPork)];
if ([self.delegate respondsToSelector:@selector(buyMilk)]) {
[self.delegate performSelector:@selector(buyMilk)];
}else{
[shoppingList addObject:[NSString stringWithFormat:@"Milk"]];
}
if ([self.delegate respondsToSelector:@selector(buyCoffee)]) {
[self.delegate performSelector:@selector(buyCoffee)];
}else{
[shoppingList addObject:[NSString stringWithFormat:@"Coffee"]];
}
[self buyStuff];
}
}
@end
for (id key in dicQuestion) {
NSLog(@"Key:%@,Value:%@",key,[dicQuestion objectForKey:key]);
}
NSArray *array = [NSArray arrayWithObjects:
@"one", @"two", @"three", @"four", nil];
for (NSString *element in array) {
NSLog(@"element: %@", element);
}
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];
NSString *key;
for (key in dictionary) {
NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]);
}
NSArray *array = [NSArray arrayWithObjects:
@"one", @"two", @"three", @"four", nil];
NSEnumerator *enumerator = [array reverseObjectEnumerator];
for (NSString *element in enumerator) {
if ([element isEqualToString:@"three"]) {
break;
}
}
NSString *next = [enumerator nextObject];
// next = "two"
NSArray *array = <#Get an array#>;
NSUInteger index = 0;
for (id element in array) {
NSLog(@"Element at index %u is: %@", index, element);
index++;
}
//http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocFastEnumeration.html#//apple_ref/doc/uid/TP30001163-CH18-SW1
//*********
// NSArray
//*********
NSArray *array1 = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
NSArray *array2 = [NSArray arrayWithObjects:@"Two", @"Four", @"One", nil];
NSMutableArray *intermediate = [NSMutableArray arrayWithArray:array1];
[intermediate removeObjectsInArray:array2];
NSUInteger difference = [intermediate count];
With that way, only common elements will be removed.
//ref: http://forrst.com/posts/Convert_seconds_to_time_in_Objective_C_iOS-XNd
//used for lx 计算付费问题剩余天数
- (NSString *)convertTimeFromSeconds:(NSString *)seconds {
// Return variable.
NSString *result = @"";
// Int variables for calculation.
int secs = [seconds intValue];
int tempHour = 0;
int tempMinute = 0;
int tempSecond = 0;
NSString *hour = @"";
NSString *minute = @"";
NSString *second = @"";
// Convert the seconds to hours, minutes and seconds.
tempHour = secs / 3600;
tempMinute = secs / 60 - tempHour * 60;
tempSecond = secs - (tempHour * 3600 + tempMinute * 60);
hour = [[NSNumber numberWithInt:tempHour] stringValue];
minute = [[NSNumber numberWithInt:tempMinute] stringValue];
second = [[NSNumber numberWithInt:tempSecond] stringValue];
// Make time look like 00:00:00 and not 0:0:0
if (tempHour < 10) {
hour = [@"0" stringByAppendingString:hour];
}
if (tempMinute < 10) {
minute = [@"0" stringByAppendingString:minute];
}
if (tempSecond < 10) {
second = [@"0" stringByAppendingString:second];
}
if (tempHour == 0) {
NSLog(@"Result of Time Conversion: %@:%@", minute, second);
result = [NSString stringWithFormat:@"%@:%@", minute, second];
} else {
NSLog(@"Result of Time Conversion: %@:%@:%@", hour, minute, second);
result = [NSString stringWithFormat:@"%@:%@:%@",hour, minute, second];
}
return result;
}
#http://www.xuanyusong.com/archives/425
#import <UIKit/UIKit.h>
#import "MyClass.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//添加我们的测试代码
//创建集合
NSSet *set = [NSSet setWithObjects:@"雨松MOMO",@"15810463139",[NSNumber numberWithInteger: 9527], nil];
//集合的数量
int count = [set count];
NSLog(@"集合中的数量: %d",count);
//检测包含
NSString * str = @"雨松MOMO";
if ([set containsObject:str]) {
NSLog(@"集合中包含 %@这个对象", str);
}
//迭代器遍历
NSEnumerator *enumerator = [set objectEnumerator];
NSObject *object = [enumerator nextObject];
while (object != nil) {
NSLog(@"迭代器遍历集合中的数据: %@",object);
object = [enumerator nextObject];
}
//快速枚举遍历
for (NSObject *object in set) {
NSLog(@"快速枚举遍历集合中的数据: %@",object);
}
//创建集合
NSMutableSet *set = [NSMutableSet setWithCapacity:10];
//添加数据
[set addObject:@"雨松MOMO"];
[set addObject:[NSNumber numberWithInt:9527]];
//添加数据在删除
NSString *str = @"删除我";
[set addObject:str];
//删除它
[set removeObject:str];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
//**********************************
//Convert NSString to NSDictionary
//**********************************
NSString *responseData = [request responseString];
NSDictionary *responseDic = [responseData objectFromJSONString];
//trim
NSString * string = @"\n \t\t hello string \t\n \n\n";
NSString * trimed = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
NSLog(@"%@", trimed);
NSString *hello=@"Hello,";
NSString *world=@"World!";
NSString *helloWorld;
//将hello和world这两个字符串连接。三种方法:
helloWorld = [NSString initWithFormat:@"%@%@", hello, world ];
helloWorld= [helloWorld stringByAppendingFormat:@"%@%@",hello, world];
helloWorld = [hello stringByAppendingString:world]; //高效
//字符串以开头和结尾比较
NSString *str0 = @"xxd79";
NSString *str1 = @"xxd79";
if([str0 hasPrefix:@"xxd"])
{
NSLog(@"字符串str0以xxd开头");
}
if([str1 hasSuffix:@"79"])
{
NSLog(@"str1字符串以79结尾");
}
//*****************
//ternary operator
//*****************
NSLog(@"%@", boolVal ? @"YES" : @"NO");
NSString* title = [[DBSession sharedSession] isLinked] ? @"Unlink Dropbox" : @"Link Dropbox";
- (void)loadView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请检查您的登录信息" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
[alert show];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
NSLog(@"Cancel Button Pressed");
break;
case 1:
NSLog(@"Button 退出 Pressed");
default:
break;
}
}
- (void)loadView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请检查您的登录信息" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
[alert show];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
NSLog(@"Cancel Button Pressed");
break;
case 1:
NSLog(@"Button 退出 Pressed");
default:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment