在本文中,我们将为您详细介绍objective-c–为什么我可以更改此constNSMutableSring?的相关知识,并且为您解答关于const为什么可以修改对象的疑问,此外,我们还会提供一些关于
在本文中,我们将为您详细介绍objective-c – 为什么我可以更改此const NSMutableSring?的相关知识,并且为您解答关于const为什么可以修改对象的疑问,此外,我们还会提供一些关于ios – 如何将NSMutableArray添加到NSMutableArray Objective-c、iphone – Objective-C:按工作日排序NSDates的NSMutableArray、NSMutableArray 可变数组-Objective-C、NSMutableString 可修改字符串 - Objective-C的有用信息。
本文目录一览:- objective-c – 为什么我可以更改此const NSMutableSring?(const为什么可以修改对象)
- ios – 如何将NSMutableArray添加到NSMutableArray Objective-c
- iphone – Objective-C:按工作日排序NSDates的NSMutableArray
- NSMutableArray 可变数组-Objective-C
- NSMutableString 可修改字符串 - Objective-C
objective-c – 为什么我可以更改此const NSMutableSring?(const为什么可以修改对象)
const NSMutableString *const foobar = [[NSMutableString alloc] initWithFormat:@"Hello"]; [foobar appendString:@" World"]; NSLog(@"String: %@",foobar);
它输出:
String: Hello World
我的变量不应该是const(以及指针)吗?因此我不能修改它.
C的行为与我期望的一样
int main() { const std::string *const s = new std::string("hello"); s->append(" world"); std::cout << s << std::endl; }
输出:
$g++ test.c++ test.c++: In function ‘int main()’: test.c++:7:20: error: passing ‘const string {aka const std::basic_string<char>}’ as ‘this’ argument of ‘std::basic_string<_CharT,_Traits,_Alloc>& std::basic_string<_CharT,_Alloc>::append(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ discards qualifiers [-fpermissive] s->append(" world");
我知道我不应该使用const和mutable,但这并没有改变事实,即mutable应该是const.
解决方法
这在技术上不是偏离C()中const的语义,因为C()中没有Objective-C类类型,因此它们不被C()标准所涵盖.
Objective-C本身并没有任何正式的描述,足够标准. Objective-C 2.0编程语言,2009,没有提到常量语义,也很久以来作为参考被遗弃并且没有被替换.
换句话说,我无法指引您查看任何明确的文档,该文档声明const默默地省略了Objective-C类类型,但这就是编译器的行为方式.
如果你想要“常量”对象,你必须遵循Objective-C可变/不可变类模式.
ios – 如何将NSMutableArray添加到NSMutableArray Objective-c
我有一个NSMutableArray存储NSMutableArrays。如何将数组添加到数组?
解决方法
[myArray addobject:otherArray];
或连接数组。
[myArray addobjectsFromArray:otherArray];
这两个都记录在the documentation。
iphone – Objective-C:按工作日排序NSDates的NSMutableArray
NSMutableArray *openingHoursArray = [NSMutableArray arrayWithArray: [[museum valueForKey:@"openingHours"] allObjects]];
数组未排序,因为从我的NSManagedobject“博物馆”获取键的值会返回一个随机组织的集合.
数组中的NSManagedobjects“openingHours”具有NSDate类型的值,可通过键“start”和“end”访问.现在,以一种有意义的方式显示我的营业时间,我访问相应日期的工作日和时间信息,到目前为止使用这段代码工作正常:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:mainDelegate.localeCode]; NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease]; [weekday setLocale:locale]; [weekday setDateFormat: @"EEEE"]; [NSDateFormatter *time = [[[NSDateFormatter alloc] init] autorelease]; [time setLocale:locale]; [time setTimeStyle:NSDateFormatterShortStyle]; for (NSManagedobject *openingHour in openingHoursArray) { NSLog(@"%@",[openingHour valueForKey:@"start"]); NSDate *startDate = [openingHour valueForKey:@"start"]; NSDate *endDate = [openingHour valueForKey:@"end"]; Nsstring *startDay = [weekday stringFromDate:startDate]; Nsstring *endDay = [weekday stringFromDate:endDate]; Nsstring *startTime = [time stringFromDate:startDate]; Nsstring *endTime = [time stringFromDate:endDate]; if (![startDay isEqualToString:endDay]) { [openingHouRSString appendFormat:@"%@ - %@:\t%@ - %@ \n",startDay,endDay,startTime,endTime]; } else { [openingHouRSString appendFormat:@"%@:\t%@ - %@ \n",endTime]; } }
这给了我想要的输出:
周四:上午10:00 – 晚上9:00
周五 – 周日:上午10:00 – 下午6:00
周一至周三:上午10:00至下午5:00
现在的问题是:我当然希望首先显示星期一的开放时间.我需要按开始日期值的工作日对数组进行排序.我尝试使用以下方法对其进行排序:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"start" ascending:TRUE]; [openingHoursArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
但显然这不起作用,因为它只按日期排序,并且不幸的是1970-01-01星期四,因此星期四将是我的数组的第一项按上面的代码排序.
有没有人对如何“解决这个问题”有一个好主意? :-)我完全坚持这个..
非常感谢!
解决方法
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSArray *sortedArray = [openingHoursArray sortedArrayUsingComparator: ^(id obj1,id obj2) { NSDate *startDate1 = [obj1 valueForKey:@"start"]; NSDate *startDate2 = [obj2 valueForKey:@"start"]; NSDateComponents *components1 = [calendar components:NSWeekdayCalendarUnit fromDate:startDate1]; NSDateComponents *components2 = [calendar components:NSWeekdayCalendarUnit fromDate:startDate2]; NSInteger weekday1 = [components1 weekday]; if (weekday1 == 1) weekday1 = 8; //Order sundays last NSInteger weekday2 = [components2 weekday]; if (weekday2 == 1) weekday2 = 8; return [[NSNumber numberWithInteger:weekday1] compare:[NSNumber numberWithInteger:weekday2]]; }];
NSMutableArray 可变数组-Objective-C
NSArray 是一个静态数组,不修改该数组中的元素,也不能添加元素,所以仅有NSArray 是不够的。NSMutableArray 就应运而生,来满足我们的需求了。
array | 创建一个空数组 |
+(id)arrayWithCapacity:size | 创建一个数组,指定容量为size |
-(id)initWithCapacity:size | 初始化一个新分配的数组,指定容量为size |
-(void)addObject:obj | 将对象obj添加到数组末尾 |
-(void)insertObject:obj atIndex:i | 将对象 obj 插入到索引为 i 的位置 |
-(void)replaceObject:obj atIndex:i | 将数组中索引为 i 处的元素用obj 置换 |
-(void)removeObject:obj | 从数组中删除所有是 obj 的对象 |
-(void)removeObjectAtIndex:i | 从数组中删除索引为 i 的对像 |
-(void)sortUsingSelector:(SEL)selector | 用 selector 只是的比较方法将数组排序 |
原文链接: http://blog.csdn.net/iukey/article/details/7341768
NSMutableString 可修改字符串 - Objective-C
+(id)stringWithCapacity:size | 创建一个字符串,容量为 size 大小 |
-(id)initWithCapacity:size | 初始化一个字符串,容量为 size |
-(void)setString:nsstring | 将字符串设置为 nsstring |
-(void)appendString:nsstring | 在字符串末尾追加字符串 nsstring |
-(void)deleteCharatersInRange:range | 删除指定 range 中的字符 |
-(void)insertString:nsstring atIndex:i | 以索引 i 为起始位置插入 nsstring |
-(void)replaceCharatersInRange;range withString:nsstring | 使用 nsstring 替换 range 指定的字符 |
-(void)replaceOccurrencesOfString:nsstring withString:nsstring2 options:opts range:range | 根据选项 opts ,使用指定 range 中的 nsstring2 替换所有的 nsstring |
原文链接: http://blog.csdn.net/iukey/article/details/7341681
关于objective-c – 为什么我可以更改此const NSMutableSring?和const为什么可以修改对象的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ios – 如何将NSMutableArray添加到NSMutableArray Objective-c、iphone – Objective-C:按工作日排序NSDates的NSMutableArray、NSMutableArray 可变数组-Objective-C、NSMutableString 可修改字符串 - Objective-C等相关知识的信息别忘了在本站进行查找喔。
本文标签: