foundation

Why do Objective-C objects have to be dynamically allocated?

霸气de小男生 提交于 2019-12-30 01:53:04
问题 Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks. 回答1: the primary reason: not knowing how much stack size to reserve. existing conventions and uses also make lifting the restriction quite difficult. dynamic messaging does not matter in this case, as setting the right 'vtable' at initialization is trivial. in c++, the size of a stack object is always known (and if it's wrong, you know

DateFormatter returns unexpected date for Timezone

余生长醉 提交于 2019-12-29 09:19:08
问题 I've read multiple posts about this, and most were due to not setting a specific Timezone and the system getting the current one. My scenario is slightly different and gives unexpected results, so I'll try to explain in detail. I'm dealing with a backend that returns dates split into a date and a time, stored as an Integer value. For example, a return value could be 131006 , which would translate to 13:10:06 . I know that this backend always returns the date in UTC . I cast this to a String

SEGV_ACCERR calling [[NSNotificationCenter defaultCenter] removeObserver:self] in dealloc

我只是一个虾纸丫 提交于 2019-12-29 08:13:09
问题 I'm really at a loss as to how this happened. I have an app that uses ARC. Most of the my view controllers register for NSNotifications. All registrations are done on the main thread. When a memory warning occurs, the navigation controller used for each non-visible tab is nil'd and is consequently deallocated. In this case, one navigation controller and its view controller were deallocated, and the view controller crashed the app during its dealloc method. Specifically, it was removing itself

Memory management of Objective-C 2.0

笑着哭i 提交于 2019-12-25 03:49:29
问题 I'm a beginner in Objective-C. I'm studying memory management in Objective-C by writing simple command line code. My environment is below. Mac OS X Mountain Lion. Xcode4.5 I wrote a code below. test.m 1 #import <Foundation/Foundation.h> 2 #import <stdio.h> 3 4 @interface A : NSObject 5 -(void)myprint; 6 @end 7 8 @implementation A 9 -(void)dealloc { 10 printf("dealloc!!\n"); 11 [super dealloc]; 12 } 13 14 -(void)myprint { 15 printf("myprint!!\n"); 16 } 17 @end 18 19 int main(void) { 20 21 id

Convert NSData to NSString and ignore null bytes?

让人想犯罪 __ 提交于 2019-12-25 02:50:52
问题 I need to convert a NSData object to an NSString. It is meant to be gibberish but I need it for debbuging. When I use NSString's initWithData, it breaks as the data has NULL bytes. How can I make it ignore the null bytes and get a proper string? 回答1: Copy the NSData to a NSMutableData, byte by byte, skipping any null bytes. Then initialise the string from that. 来源: https://stackoverflow.com/questions/3949628/convert-nsdata-to-nsstring-and-ignore-null-bytes

How to I restore deleted UIKit.framework and Foundation.framework and others

泄露秘密 提交于 2019-12-24 08:31:04
问题 I deleted UIKit.framework and Foundation.framework in an xcode project by accident. I can't find the framework in the target information (general tab) link libraries section. How could I add them to the project again as I'm now getting many errors! 回答1: Project > Edit Active Target > General tab, click the + button to add frameworks. 来源: https://stackoverflow.com/questions/1462123/how-to-i-restore-deleted-uikit-framework-and-foundation-framework-and-others

How does NSData's implementation of the hash method work?

做~自己de王妃 提交于 2019-12-24 00:49:54
问题 When calling hash on the built in NSData class in the foundation framework -- what implementation is used to return the hash value? (CRC32, something else?) 回答1: Something else. Actually it's an implementation detail, which does not need to use a fixed algorithm in different versions. You can check the implementation in the open source version of Core Foundation. Note that NSData is toll-free bridged to CFDataRef. From http://opensource.apple.com/source/CF/CF-635.21/CFData.c: static

NSLocaleCountryCode returns nil

China☆狼群 提交于 2019-12-23 07:28:58
问题 I have a bugreport that states a crash in the following line, where client is an instance of NSMutableDictionary [client setObject:[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] forKey:@"country"]; My guess is, that NSLocaleCountryCode returns nil in this line, which leads to adding a nil object to an NSDictionary which would lead to a crash. The question is, has anybody experienced an issue like this before? Are there any reasons NSLocaleCountryCode could be nil for the

NSLocaleCountryCode returns nil

给你一囗甜甜゛ 提交于 2019-12-23 07:28:10
问题 I have a bugreport that states a crash in the following line, where client is an instance of NSMutableDictionary [client setObject:[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] forKey:@"country"]; My guess is, that NSLocaleCountryCode returns nil in this line, which leads to adding a nil object to an NSDictionary which would lead to a crash. The question is, has anybody experienced an issue like this before? Are there any reasons NSLocaleCountryCode could be nil for the

Arithmetic Operators and Key-Value Coding

倖福魔咒の 提交于 2019-12-23 02:39:05
问题 Is it possible to do arithmetics using Obj-C Key-Value Coding? I am looking for something like this: [obj valueForKeyPath:@"(val1+val2)"] or do you have to implement a property that adds the two values manually? 回答1: Arithmetic using KVC is not possible. You can do arithmetic with strings and then pass it to [obj valueforKeyPath:...] as : NSString *val1=@"3"; NSString *val2=@"5"; NSString *formula = [NSString stringWithFormat:@"%@+%@",val1,val2]; NSExpression *exp = [NSExpression