Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

iOS中model的随笔

$
0
0

先设定model的元素有code,name

CodeName.h

#import <Foundation/Foundation.h>

@interface CodeName : NSObject

@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *name;

+ (CodeName*)codeNameWithCode:(NSString*)code name:(NSString*)name;

@end

CodeName.m

#import "CodeName.h"

@implementation CodeName

- (void)dealloc
{
    self.code = nil;
    self.name = nil;
    [super dealloc];
}

- (BOOL)isEqual:(id)object
{
    if (object == self) {
        return YES;
    }
    if (![object isKindOfClass:[self class]]) {
        return NO;
    }
    CodeName *codeName = (CodeName *)object;
    return [codeName.code isEqualToString:self.code];
}

+ (CodeName*)codeNameWithCode:(NSString*)code name:(NSString*)name
{
    CodeName *codeName = [[[CodeName alloc] init] autorelease];
    codeName.code = code;
    codeName.name = name;
    return codeName;
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"code=%@,name=%@", _code, _name];
}

@end

快速索引的使用示例:

    CodeName *codeName1 = [CodeName codeNameWithCode:@"code1" name:@"name1"];
    NSLog(@"%@", codeName1);
    CodeName *codeName2 = [CodeName codeNameWithCode:@"code2" name:@"name2"];
    CodeName *codeName3 = [CodeName codeNameWithCode:@"code3" name:@"name3"];
    CodeName *codeNameTest = [CodeName codeNameWithCode:@"codeTest" name:@"name3"];
    NSArray *array = [NSArray arrayWithObjects:codeName1, codeName2, codeName3, nil];
    BOOL flag = [array containsObject:codeName1];
    NSLog(@"codeName_1:%@", flag?@"存在":@"不存在");
    flag = [array containsObject:codeNameTest];
    NSLog(@"codeName_Test:%@", flag?@"存在":@"不存在");
    
    
    NSInteger index = [array indexOfObject:codeNameTest];
    if (index == NSNotFound) {
        NSLog(@"codeNameTest NSNotFound");
    }
    else {
        NSLog(@"codeName1:index=%d", index);
    }
    index = [array indexOfObject:codeName1];
    if (index == NSNotFound) {
        CFShow(@"codeNameTest NSNotFound");
    }
    else {
        NSLog(@"codeNameTest:index=%d", index);
    }

打印结果:

2013-02-07 22:40:25.724 Test[1576:c07] code=code1,name=name1
2013-02-07 22:40:25.726 Test[1576:c07] codeName_1:存在
2013-02-07 22:40:25.727 Test[1576:c07] codeName_Test:不存在
2013-02-07 22:40:25.727 Test[1576:c07] codeNameTest NSNotFound
2013-02-07 22:40:25.728 Test[1576:c07] codeNameTest:index=0

iOS的模糊查询

    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*me1*'"];
    NSArray *results = [array filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);

打印结果:

2013-02-07 22:44:53.702 Test[1612:c07] (
    "code=code1,name=name1"
)



作者:z251257144 发表于2013-2-7 22:35:31 原文链接
阅读:63 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>