UIImage converts to NSData mysteriously - objective-c

Please take a look at these two simple pieces of code. This
- (void)testMethod
{
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"myEncodedObjectKey"];
self = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data];
for (int i = 0; i < self.avatar.count; i++)
[self.avatar replaceObjectAtIndex:i withObject:[UIImage imageWithData:[self.avatar objectAtIndex:i]]];
if ([[self.avatar objectAtIndex:0] isKindOfClass:[UIImage class]])
NSLog(#"UIImage");//at this moment it's UIImage
}
and this:
[currentProfile testMethod];
if ([[currentProfile.avatar objectAtIndex:0] isKindOfClass:[NSData class]])
NSLog(#"NSData");//Moment later it is NSData
In the first one i fetch a custom object from the NSUserDefaults and work with a NSMutableArray variable named "avatar". I convert its each object from NSData to UIImage. Then i check what i've got by using NSLog . It's UIImage. At the second piece of code you can see how a moment later what was UIImage turns back to NSData by its own will. Seems like i described my issue clearly. Do you understand what's going on? I don't. Thanks a lot in advance for your attention

Why are you changing the self object in your -testMethod method? This is highly illegal.
What you're actually doing is setting a local variable self, which is passed as a parameter to your method, to a new value. This means you're not editing the receiver of the method, you're just editing your parameter.
When your method is called at runtime the C function objc_msgSend() is called:
// Declaration of objc_msgSend
id objc_msgSend(id receiver, SEL selector, ...);
Now when you call your method ...
[myInst testMethod];
... this is what actually gets called at runtime:
objc_msgSend(myInst, #selector(testMethod));
Do you already see what's happening? In your method implementation the self variable is set to the first argument of objc_msgSend. When you're reassigning self, your not editing what the variable myInst contains and thus you're not editing your the original instance you passed. You're just setting myInst, aka self, a local variable, to your knew pointer. The caller of the function will not notice the change.
Compare you're code to the following C code:
void myFunction(int a) {
a = 3;
}
int b = 2;
myFunction(b);
printf("%d\n", b);
// The variable b still has the original value assigned to it
The above code does the same you do:
// Variation on objc_msgSend
void myMethodWrittenInC(id myInst) {
// Local variable changes, but will not change in the calling code
myInst = nil;
}
MyClass *myObj;
myObj = [[MyClass alloc] init];
myMethodWrittinInC(myObj);
// At this point myObj is not nil
And finally this is what you do:
- (void)testMethod
{
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"myEncodedObjectKey"];
// You assign the local variable self (passed as an invisible argument
// to your method) to your new instance, but you do not edit the original
// instance self pointed to. The variable currentProfile does not change.
self = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data];
for (int i = 0; i < self.avatar.count; i++)
[self.avatar
replaceObjectAtIndex:i
withObject:[UIImage imageWithData:[self.avatar objectAtIndex:i]]];
if ([[self.avatar objectAtIndex:0] isKindOfClass:[UIImage class]])
NSLog(#"UIImage");//at this moment it's UIImage
}
// (1) Here currentProfile points to an instance of your class
[currentProfile testMethod];
// (2) it calls the method, but the local variable does not change
// and still points to the same instance.
if ([[currentProfile.avatar objectAtIndex:0] isKindOfClass:[NSData class]])
NSLog(#"NSData");//Moment later it is NSData

Related

__weak NSString *text = self.textField.text has inconsistent behaviour

I think I may made a silly mistake, but I can't figure out why:
I have a method and Block to handle some network API like:
-(IBAction)confirm:(id)sender {
__weak typeof(self) weakSelf = self;
__weak NSString *anotherNumber = self.nextPhoneTextField.text;
[SharedInstance bindNewPhoneNumber:self.nextPhoneTextField.text pinCode:self.verifyCodeTextField.text sucess:^(id result) {
// update phone number
SharedInstance.phoneNumber = anotherNumber;
}];
}
before the block, I can see newNumber has value correctly,
However when the block is invoked, the newNumber is nil, instead of the text. But I was able to print weakSelf.nextPhoneTextField.text, which is not changed.
Any explainations is appreciated!
UPDATE:
After creating a sample project, I found it's not reproducible. the weak string pointer has valid text. Then I start debugging it, and I found that,
In order to avoid the new keyword, I changed the pointer name to anotherNumber
In my real project, when calling __weak NSString *anotherNumber = self.nextPhoneTextField.text; the anotherNumber has a new address, rather than the self.nextPhoneTextField.text; address:
(lldb) p anotherNumber
(__NSCFString *) $2 = 0x00007f88b3ff2960 #"12345678901"
(lldb) p self.nextPhoneTextField.text
(__NSCFString *) $3 = 0x00007f88b15f8690 #"12345678901"
However in the sample project, I have the similar function,
- (void)clickBlock:(void (^)(NSString * string))block {
if (block) {
block(#"haha");
}
}
- (IBAction)clicked:(id)sender {
__weak typeof(self) weakSelf = self;
__weak NSString *text = self.textField.text;
[self clickBlock:^(NSString *string) {
NSLog(text);
NSLog(string);
}];
}
it is the same address:
(lldb) p text
(NSTaggedPointerString *) $2 = 0xa000000747365744 #"test"
(lldb) p self.textField.text
(NSTaggedPointerString *) $3 = 0xa000000747365744 #"test"
and the class type changed also... Looking for answers!!!
Another update:
I delete the block, simply create two weak pointers with some strings like "hello" and "12345678901", the formmer one has the same address and marked as NSTaggedPointerString, however the latter one has different address and marked as NSCFString
It seems to me that once the text reach a specific length, it will have the NSCFString and different address, and after some tests, the bounty is 9. once more than 9 words, it will be NSCFString, tested on iOS 9.1 iPhone 6S simulator.
on iOS 8.4 simulator, all the strings with different length result in different mem adress and NSCFString
sample project:https://github.com/liuxuan30/WeakStringPointer
__weak NSString *anotherNumber = self.nextPhoneTextField.text;
with this line , NSString is copied by value and NOT by reference, so after assigning a NSString to another NSString , it creates new copy of it and creates a reference to newly created copy and not the original one, and because the reference is weak , the object will be nil after the current function goes out of context ,
If you try to change the textfield's text it will only change the textFields text and not anotherNumber object.
NSString *test = self.nextPhoneTextField.text;
self.nextPhoneTextField.text = #"Something else";
NSSLog(#"Test object contains %# , the textField contains %# ",test,self.nextPhoneTextField.text);
Your code does following:
Creates a new Copy NSString from self.nextPhoneTextField.text
Asigns a new Copy NSString to anotherNumber
Since anotherNumber is __weak , it will not retain the object (NSString), it will hold __weak reference to this object and after this function goes out of context and it becomes nil.
To confirm this behavior, you can directly log anotherNumber after setting its value and in different context
__weak NSString *anotherNumber = self.nextPhoneTextField.text;
NSString *strongAnotherNumber = self.nextPhoneTextField.text;
NSLog(#"Weak number - %# , strong - %#",anotherNumber,strongAnotherNumber);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Block Weak number - %# , strong - %#",anotherNumber,strongAnotherNumber);
});
please also have a look at Why do weak NSString properties not get released in iOS?

Better way than write dozens of empty getters?

I use lazy instantiation on my properties, to have my class created and used as fast as possible. To achieve this, I write lots of 'empty' getters like this:
- (VMPlacesListFilter *)currentFilter
{
if (!_currentFilter) {
_currentFilter = [[VMPlacesListFilter alloc] init];
}
return _currentFilter;
}
They are all the same: if the instance variable is nil, call the -alloc and -init on the class of the property, then return the instance variable. Very common and straightforward.
If I don't create this getter by myself, Objective-C's automatic synthesization creates a getter for me, which does only the returning part (does not init the object if the instance variable is nil).
Is there any way to avoid writing this boilerplate code?
Nope, I'm afraid there's no good way around it, if you really want to have lazy initialization. Personally, I usually save lazy initialization for stuff that could really be time consuming or memory intensive (say, loading images or view controllers), and initialize cheap stuff (like simple data structures or model objects) in init.
- (instancetype) init {
self = [super init];
if( self ) {
_cheapThing1 = [NSMutableArray array];
_cheapThing2 = [[MyModelObject alloc] init];
}
return self;
}
- (ExpensiveThing*) expensiveThing
{
if( _expensiveThing == nil ) {
_expensiveThing = [[ExpensiveThing alloc] init];
}
return _expensiveThing;
}
Unless you're loading something from disk or the network, I wouldn't worry too much about initialization time. Of course, profile it.
I know this is an Objective-C question, but it's worth noting that Swift has lazy initialization built-in.
lazy var currentFilter = VMPlacesListFilter()
First off, I totally agree with #zpasternack that "lazy load" should not be misused. However, automatically generating setters and getters is completely doable with the power of Objective-C runtime. In fact, CoreData is doing this.
Anyway, I have come up with some stupid code implementing a class called LazyClass, in which you can declare dynamic properties like lazyArray (see below). Using dynamic method resolution, when the property is accessed for the first time, a getter that calls the corresponding class's default +alloc and -init method will be automatically added to the class. All underlying instance variables are stored in an NSMutableDictionary called myVars. Of course you can manipulate ivars through the runtime API as well, but using a dictionary should save some work.
Please note that this implementation just shows the basic idea of how it works. It lacks error checking and is not supposed to be shipped.
LazyClass.h
#interface LazyClass : NSObject
#property NSMutableDictionary *myVars;
// lazily initialized property
#property NSArray *lazyArray;
#end
LazyClass.m
#import "LazyClass.h"
#import <objc/objc-runtime.h>
#implementation LazyClass
#dynamic lazyArray;
- (instancetype)init {
self = [super init];
self.myVars = [NSMutableDictionary dictionary];
return self;
}
- (NSMutableDictionary *)getMyVars {
return self.myVars;
}
// the generated getter method
id dynamicGetterMethodIMP(id self, SEL _cmd) {
// selector name, which is also the property name
const char *selName = sel_getName(_cmd);
NSString *selNSName = [NSString stringWithCString:selName encoding:NSUTF8StringEncoding];
NSString *keyPath = [NSString stringWithFormat:#"myVars.%#", selNSName];
if (![self valueForKeyPath:keyPath]) {
// get the actual type of the property
objc_property_t property = class_getProperty([self class], selName);
const char *attr = property_getAttributes(property);
NSString *attrString = [[NSString alloc] initWithCString:attr encoding:NSUTF8StringEncoding];
NSString *typeAttr = [[attrString componentsSeparatedByString:#","] firstObject];
NSString *typeName = [typeAttr substringWithRange:NSMakeRange(3, typeAttr.length - 4)];
// the default initialization
Class typeClass = NSClassFromString(typeName);
[self setValue:[[typeClass alloc] init] forKeyPath:keyPath];
}
return [self valueForKeyPath:keyPath];
}
// the generated setter method
void dynamicSetterMethodIMP(id self, SEL _cmd, id value) {
// get the property name out of selector name
// e.g. setLazyArray: -> lazyArray
NSString *propertyName = NSStringFromSelector(_cmd);
propertyName = [propertyName stringByReplacingOccurrencesOfString:#"set" withString:#""];
propertyName = [propertyName stringByReplacingOccurrencesOfString:#":" withString:#""];
propertyName = [NSString stringWithFormat:#"%#%#", [propertyName substringToIndex:1].lowercaseString, [propertyName substringFromIndex:1]];
NSString *keyPath = [NSString stringWithFormat:#"myVars.%#", propertyName];
[self setValue:value forKeyPath:keyPath];
}
// dynamic method resolution
+ (BOOL)resolveInstanceMethod:(SEL)aSEL {
if ([NSStringFromSelector(aSEL) containsString:#"set"]) {
class_addMethod([self class], aSEL, (IMP)dynamicSetterMethodIMP, "^?");
} else {
class_addMethod([self class], aSEL, (IMP)dynamicGetterMethodIMP, "v#:");
}
return YES;
}
#end
Documentation
If it's the verboseness that bothers you, I suppose you could compress lazy initialisers that only need one-line initialization using the ternary operator:
- (VMPlacesListFilter *)currentFilter
{
return _currentFilter ? : (_currentFilter = [[VMPlacesListFilter alloc] init]);
}
DISCLAIMER: I don't do this, but it's interesting that it can be done

Using typecast for (void *) in Objective C

i am using the addToolTipRect: method to set a tooltip rect
- (NSToolTipTag)addToolTipRect:(NSRect)aRect owner:(id)anObject userData:(void *)userData
and method stringForToolTip: to obtain string value for tooltip.
- (NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)data
However the above functions work fine if i send something like
[self addToolTipRect:someRect owner:self userData:#"Tool tip string"];
But doesn't work when i send the following string. Error: BAD_ACCESS
const NSString * tooltipStr = #"Tool tip string";
[self addToolTipRect:someRect owner:self userData:tooltipStr];
In both the cases, the stringForToolTip looks like:
- (NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)data
{
id obj = (id)data;
NSString * str=nil;
if ([obj isKindOfClass:[SomeClass class]]) //This is my system defined class and works fine
{
SomeClass * someClassObj = (SomeClass *) data;
str = someClassObj.title;
}
else if([obj isKindOfClass:[NSString class]])
str = (NSString*)obj;
return str;
}
NOTE: In the stringForToolTip: method I also want to check for some other class example [obj isKindOF:[SomeClass class]] and i don't want to assert that value. The problem here is just in getting the string value by proper cast but I can't figure out how! Please tell me where I am going wrong?
edit:
What should be the right way to get the String value for tooltip in that case? should the point or tag be considered?
(void *) is not an object pointer.
That #"Tool tip string" worked was by coincidence based on the fact that is is a compile-time constant with a (essentially) permanent allocation and permanent address.
But in the code:
const NSString * tooltipStr = #"Tool tip string";
[self addToolTipRect:someRect owner:self userData:tooltipStr];
tooltipStr is an object that is kept in memory by a strong reference (retain count > 0). Since userData: does not handle objects it does not make a strong reference (does not increase the retain count) so it is released, will disappear soon becoming invalid.
Notes from the documentation:
The tooltip string is obtained from the owner. The owner must respond to one of two messages, view:stringForToolTip:point:userData: or description, use the latter. Note that NSString responds to description so you can pass an NSString for the value of owner. So, what you want is: [self addToolTipRect:someRect owner:tooltipStr userData:NULL];. There is still an issue that something must hole a strong reference to the NSString instance.
You can: [self addToolTipRect:someRect owner:#"Tool tip string" userData:NULL];
Probably the best way to go is to pass self as owner and NULL as data and implement the delegate method: view:stringForToolTip:point:userData: in the class.

Retrieving Class Object from NSArray and adding to self

Hi im trying to retrieve an object of a specific class from an NSMutableArray, and then add it to self: eg:
- (void) init{
_Objects = [[NSMutableArray alloc]init];
Psychicing *psy = [[Psychicing alloc]init];
[psy startPsychic];
[_Objects addObject: psy];
[psy release];
}
This creates an object of class Psychicing, then runs the [psy startPsychic] method to create the internals of the class object. Then I add the psy object to _Objects NSMutableArray.
-(void)startPsychic{
id psychicParticle = [CCParticleSystemQuad ......]; //is Synthesised with (assign)
//Other things are set here such as position, gravity, speed etc...
}
When a touch is detected on screen, I want to take the psy object from the _Objects array and add it to self: Something like this (Although this gives runtime error)
-(void) Touches.....{
for (Psychicing *psy in _Objects){
[self addChild: psy.psychicParticle];
}
}
I hope i have explained it clearly enough, if you need more clarification let me know.
So basically:
[MainClass Init] -> [Psychicing startPsychic] -> [MainClass add to array] -> [MainClass add to self]
I'm assuming the _Objects (which should be a lowercase o to follow conventions) is storing objects other than the Psychicing object and you're trying to pull just the Psychicing object out of it in the -(void)Touches... method (which also should be lowercase). If so, you could do:
for (id obj in _Objects)
{
if ([obj isMemberOfClass:[Psychicing class]])
[self addChild:obj.psychicParticle];
}
That will cause only the Psychicing objects in the array to be added as a child to self.
It looks like you do have another error though if the code you pasted in is your real code. Init should be defined as:
- (void) init{
_Objects = [[NSMutableArray alloc]init];
Psychicing *psy = [[Psychicing alloc]init];
[psy startPsychic];
[_Objects addObject: psy];
[psy release];
}
with _Objects defined as an instance variable (or property) in the class's interface. As you wrote it, it's a method variable in the init method and is leaking. So when you try to access _Objects in -touches, _Objects is most likely nil.
Okay, with the help of McCygnus I got it working, the only thing missing with a pointer to the id object:
for (id obj in _Objects){
if ([obj isMemberOfClass:[Psychicing class]]){
Psychicing *apsy = obj;
[apsy.psychicParticle setPosition:location];
[self addChild:apsy.psychicParticle];
}
}

why the tableview doesn't show binding data?

Here's my code of generating data
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[array initWithCapacity:20];
}
- (IBAction) readlog:(id)sender {
for (int i = 0; i < 20; i++) {
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:[path stringByAppendingFormat:#"/%d.log",i]];
[array addObject:d];
}
}
- (IBAction) writelog:(id)sender {
for (int i = 0; i < 20; i++) {
NSMutableDictionary *d = [NSMutableDictionary dictionary];
NSString *name = [NSString stringWithFormat:#"testfile%d", i];
[d setObject:[NSDate date] forKey:#"date"];
[d setObject:[path stringByAppendingFormat:#"/%d.log", i] forKey:#"path"];
[d setObject:name forKey:#"name"];
[d writeToFile:[path stringByAppendingFormat:#"/%d.log", i] atomically:YES];
}
and I bind my tableview column with appdelegate.array with keypath name/path/date
but it doesn't show any data in the array.. is there anything wrong here?
Thanks!
You haven't created an array.
init methods, including NSMutableArray's initWithCapacity:, initialize an existing (freshly-created) instance. You haven't created one, so you're sending that initWithCapacity: message to nil, which means it has no effect.
You need to create the array, then initialize it, then assign it to your array variable, preferably all in the same line.
There's also the issue that your table view will have already asked for the array by the time you receive the applicationDidFinishLaunching: message. You don't have one yet, so it gets nothing; by the time you create one, it has already asked you for it and gotten its answer, and does not know that it should ask again.
Create your array in init or initWithCoder: (I believe you will need the latter if your app delegate is in a nib), and implement and use Key-Value-Coding-compatible accessor methods to fill the array with values. When you send yourself accessor messages, you'll cause KVO notifications that will tip off the table view that it needs to ask for the array again. Assigning directly to the instance variable will not cause this effect.
A couple of other thingsā€¦
You have three [path stringByAppendingFormat:#"/%d.log", i] expressions in two different methods. Don't repeat yourself. Move that to a method named something like logFileNameWithLogFileNumber: and send yourself that message to generate the filename. This will make the code both clearer and easier to maintain.
Finally, as a matter of style, you should not use stringByAppendingFormat: or stringWithFormat: to construct paths. Use stringByAppendingPathComponent: (in this case, together with stringWithFormat: to generate the filename). Clarity and pathname-separator-independence are virtues.