How to address a "Implicit conversion of an Objective-C pointer to 'const void' is disallowed" error when using [NSValue valueWithPointer:t]? - cocoa

How do I fix the error "Implicit conversion of an Objective-C pointer to 'const void' is disallowed" when using the following message:
NSValue *key = [NSValue valueWithPointer:t];
Extra thanks goes to the one can offer why this code throws this exception if simple, newbie terms.
Here is the full method if it helps:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *t in touches) {
// Is this a double tap?
if ([t tapCount] > 1) {
[self clearAll];
return;
}
// Use the touch object (packed in an NSValue) as the key
NSValue *key = [NSValue valueWithPointer:t]; // here is where the error is
// Create a line for the value
CGPoint loc = [t locationInView:self];
Line *newLine = [[Line alloc] init];
[newLine setBegin:loc];
[newLine setEnd:loc];
// Put pair in dictionary
[linesInProcess setObject:newLine forKey:key];
}

I ran into the same problem working on that exercise in the second edition of the Big Nerd Range Guide, which doesn't cover ARC. That's where the issue arises. They've updated their example/solution code here: http://www.bignerdranch.com/solutions/iOSProgramming3ed.zip
Within that, you can see they've changed that line to:
NSValue *key = [NSValue valueWithNonretainedObject:t];

Try to simply add (_bridge void) !

Related

Getting "Incompatible pointer types sending 'UIImage *' to parameter of type 'CIImage * _Nonnull' with JSQMessagesViewController

JSQMessagesViewController
Using Xcode 7.0 Beta 5, I'm getting a warning of an "Incompatible pointer types sending 'UIImage *' to parameter of type 'CIImage * _Nonnull' in the copyWithZone method of JSQPhotoMediaItem.
Here's the method:
- (instancetype)copyWithZone:(NSZone *)zone
{
JSQPhotoMediaItem *copy = [[[self class] allocWithZone:zone] initWithImage:self.image];
copy.appliesMediaViewMaskAsOutgoing = self.appliesMediaViewMaskAsOutgoing;
return copy;
}
The warning is for the first line initializing the JSQPhotoMediaItem, and the initWithImage shows that it expects a (UIImage *)
- (instancetype)initWithImage:(UIImage *)image
{
self = [super init];
if (self) {
_image = [image copy];
_cachedImageView = nil;
}
return self;
}
It also reports that: "Passing argument to paramteter 'im' here" referencing CISampler.h, which indeed has an initWithImage(CIImage *) im[![enter image description here][1]][1]
Thanks.
change
JSQPhotoMediaItem *copy = [[[self class] allocWithZone:zone] initWithImage:self.image];
into
JSQPhotoMediaItem *copy = [[JSQPhotoMediaItem allocWithZone:zone] initWithImage:self.image];
will get rid of this warning.

CKSMSComposeRemoteViewController timed out waiting for fence barrier from com.apple.mobilesms.compose

Ok, so sendSMS worked fine before on ios7 and below. However, on ios8 the sendSMS function just fails with the error in the title of the question. I am getting a warning here (after trying to resolve by changing NSArray to NSString using other stack overflow questions): Incompatible pointer types assigning to 'NSArray *' from 'NSString *' for controller.recipients = recipients; It is returning a result of MessageComposeResultCancelled.
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
[self sendSMS:#"Play me on PokerBuddies.
Download the app at: https://itunes.apple.com/us/app /poker-buddies/id404168013?mt=8"
recipientList:[NSString stringWithFormat:phone, nil]];
} else {
phone = #"[None]";
}
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSString *)recipients{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText]){
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
NSLog(#"Send SMS");
}
}
I have same problem like MessageComposeController timeout issue .
I solved it by doing this.
You have to create instance variable of MFMessageComposeViewController and when you are going to present message controller you have to check if instance object is already created then do it nil and initialize that object again.So this error "CKSMSComposeRemoteViewController timed out waiting for fence barrier from com.apple.mobilesms.compose" will not come and controller open exactly.
if ([MFMessageComposeViewController canSendText]) {
if (messageComposer) {
messageComposer = nil;
messageComposer = [[MFMessageComposeViewController alloc]init];
}
messageComposer.recipients = arrPhoneNumber;
messageComposer.messageComposeDelegate = self;
messageComposer.body = #"Your text";
isMessageComposeAppear = 1;
[self presentViewController:messageComposer animated:YES completion:nil];
}

EXEC_BAD_ACCESS(code=1,address=0x10)

In my OS X app, I'm trying to save and retrieve the tag of a radio button. The error occurs on the line marked "<-HERE" in setPreferenceRotor. There is a valid tag coming in.
// PreferenceController.h
extern NSString * const myCellKey;
extern NSString * const myMatrixChangedNotification;
#interface PreferenceController:NSWindowController
{
IBOutlet NSMatrix *matrixRotor;
}
- (IBAction)setRotorTag:(id)sender;
+ (NSInteger)preferenceRotorTag;
+ (void)setPreferenceRotor:(NSInteger)matrixTag;
#end
// PreferenceController.m
NSString * const myMatrixChangedNotification = #"myRotorChanged";
#implementation PreferenceController
- (void)windowDidLoad
{
[super windowDidLoad];
[matrixRotor selectCellWithTag:[PreferenceController preferenceRotorTag]];
}
+ (NSInteger)preferenceRotorTag
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *tagAsData = [defaults objectForKey:myCellKey];
return [NSKeyedUnarchiver unarchiveObjectWithData:tagAsData];
}
+ (void)setPreferenceRotor:(NSInteger)matrixTag
{
//NSInteger mt = matrixTag;
NSData *tagAsData = [NSKeyedArchiver archivedDataWithRootObject:matrixTag]; **//<-HERE**
[[NSUserDefaults standardUserDefaults]setObject:tagAsData forKey:myCellKey];
}
You are passing a primitive (non-object) value, of type NSInteger from variable matrixTag, to a method, archivedDataWithRootObject:, which expects an object reference value. That method happily tries to use the value (which is probably the integer 16, 0x10) as an object reference, and kaboom...
Your thinking looks correct, you know you cannot store non-object values in user defaults, and so you must wrap them as objects first. It is just your way of doing so that is wrong. What you need here is to create an instance of NSNumber from your integer. You could write:
NSNumber *tagAsNumber = [NSNumber numberWithInteger:matrixTag];
[[NSUserDefaults standardUserDefaults] setObject:tagAsNumber forKey:myCellKey];
However this pattern is common enough that a shortcut is provided:
[[NSUserDefaults standardUserDefaults] setInteger:matrixTag forKey:myCellKey];
and this will create the NSNumber object for you. There is also a corresponding integerForKey: method which will unwrap the integer for you when reading.

Incompatible pointer type with NSDate

I can't see why this doesn't work. The following code block throws a warning at the addObject line: "Passing argument 1 of 'taskWithText:dueDate:' from incompatible pointer type"
- (id)init{
self = [super init];
if (self) {
taskListArray = [[NSMutableArray alloc] init];
[taskListArray addObject:[AFLTask taskWithText:"#Helloski" dueDate:[NSDate dateWithNaturalLanguageString:#"12/31/12"]]];
}
return self;
}
It's so simple I don't see why it doesn't work. It seems to match my method:
- (id)initWithText:(NSString *)newText dueDate:(NSDate *)newDueDate{
if(self = [super init]){
taskText = [newText retain];
taskDue = [newDueDate retain];
taskCompleted = NO;
}
return self;
}
+ (id)taskWithText:(NSString *)newText dueDate:(NSDate *)newDueDate{
return [[[AFLTask alloc] initWithText:newText dueDate:newDueDate] autorelease];
}
What is going on here? I'm pretty new to Objective-C (but not programming in general) and so I'm still trying to wrap my head around pointers -- but shouldn't this work?
You're going to kick yourself:
"#Helloski"
should be
#"Helloski"

Cocoa - Calling a variadic method from another variadic one (NSString stringWithFormat call)

I have a problem with [NSString strigWithFormat:format] because it returns an id, and I have a lot of code where I changed a NSString var to an other personal type. But the compiler does not prevent me that there are places where a NSString is going to be set into another type of object.
So I'm writing a category of NSString and I'm goind to replace all my calls to stringWithFormat to myStringWithFormat.
The code is :
#interface NSString (NSStringPerso)
+ (NSString*) myStringWithFormat:(NSString *)format;
#end
#implementation NSString (NSStringPerso)
+ (NSString*) myStringWithFormat:(NSString *)format {
return (NSString*)[NSString stringWithFormat:format];
}
#end
The compiler tells me that "Format not a string literal and no format arguments".
Do you see any way to make this work ?
NSString includes a method that takes in an argument list from a variadic function. Look at this sample function:
void print (NSString *format, ...) {
va_list arguments;
va_start(arguments, format);
NSString *outout = [[NSString alloc] initWithFormat:format arguments:arguments];
fputs([output UTF8String], stdout);
[output release];
va_end(arguments);
}
Some of that code is irrelevant, but the key line is NSString *output = [[NSString alloc] initWithformat:format arguments:arguments];. That's how you can construct an NSString in a variadic function/method.
In your case, your code should look something like this:
+ (NSString *)myStringWithFormat:(NSString *)format, ... {
va_list arguments;
va_start(arguments, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format arguments:arguments];
va_end(arguments);
// perform some modifications to formattedString
return [formattedString autorelease];
}
No Objective-C expert here, but the original method signature for stringWithFormat includes ellipses, which allows you to pass in the arguments that are going to be substituted to the placeholders in the format argument.
EDIT: stringWithFormat is a so-called variadic method. Here's a link to an example.
Thank you for your help.
Reading your reference documentations, I found the solution !
This works :
In the .h
#interface NSString (NSStringPerso)
+ (NSString*) strWithFormatPerso:(id)firstObject, ...;
#end
In the .m
#implementation NSString (NSStringPerso)
+ (NSString*) strWithFormatPerso:(id)firstObject, ... {
NSString* a;
va_list vl;
va_start(vl, firstObject);
a = [[[NSString alloc] initWithFormat:firstObject, vl] autorelease];
va_end(vl);
return a;
}
#end

Resources