A bit of annoying warnings that still let the app work but would like to remove
Asked Answered
D

2

6

I tried out an app to test bluetooth communication. It is a simple app that just sends a message in text form from one iDevice to another. Originally, this app had about 6 warnings but I fixed all but two. They are the same but deal with different delegates. One is for the GKPeerPickerControllerDelegate and the other for the GKSessionDelegate. Say the Picker error is for the GKPeerPickerController named picker, when you type (more complete example to follow):

picker.delegate = self;

the compiler says:

Passing '*const___strong' to parameter of incompatible type 'id'.

For the GKSession named session, typing

session.delegate = self;

makes the compiler say:

Sending '*const___strong' to parameter of incompatible type 'id'.

These only pop in the button to send and peerPickerController. I know that these warnings do not impede on the app's ability to function but I would like to completely update this for Xcode 4.2. This app was originally written for Xcode back when iOS 3.0 was new. Yes, I am a bit picky when it comes to writing or practicing code, it must not contain any errors/warnings whenever possible.

These are the code blocks where the warning occur:

-(IBAction)btnConnect:(id)sender{
    picker = [[GKPeerPickerController alloc] init];
    picker.delegate = self;  //Warning here
    picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;

    [connect setHidden:YES];
    [disconnect setHidden:NO];
    [picker show];
}

-(void)peerPickerController:(GKPeerPickerController *)PCpicker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
    self.currentSession = session;
    session.delegate = self;  //Warning here
    [session setDataReceiveHandler:self withContext:nil];
    PCpicker.delegate = nil;

    [PCpicker dismiss];
}

Edit:

The header has this:

    @interface BTViewController : UIViewController{
GKSession *currentSession;
IBOutlet UITextField *txtMessage;
IBOutlet UIButton *connect;
IBOutlet UIButton *disconnect;

GKPeerPickerController *picker;

}

Dilation answered 4/1, 2012 at 19:20 Comment(3)
+1 for trying to remove every last warning!Lutyens
Thanks, I always try to make sure that what I write has no problems. Programming for iOS is different than Windows, which I also do for work.Dilation
code that compiles clean with -Wall always has less problems than otherwise.Lutyens
B
5

I believe whatever class self is may not be adopting the GKPeerPickerControllerDelegate and GKSessionDelegate formal protocols. Can you post your interface header?

EDIT

Casting to id will clear the warnings, but you really didn't "fix" anything...looking at the class header, it is not adopting the protocols that the delegates are expecting.

Modify your interface to adopt those protocols:

@interface BTViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate> {
Balinese answered 4/1, 2012 at 19:54 Comment(6)
If possible, how would it adopt the formal protocols? This would be helpful in understanding more about programming for iOS.Dilation
My answer shows you how add the protocols to your class. I recommend reading Apple's guide to Protocols for further understanding - developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/…Balinese
Thank you, this did adopt the protocols rather than having to cast self as an id. The warnings did disappear. The example I got this from did not have this in the header. I think they were hiding it.Dilation
As CodaFi pointed out, I am using ARC. Did this affect how things worked? Should I have not used ARC?Dilation
ARC doesn't play a role in this situation. Objective-c is not a strongly typed language and you passed an object that happened to have the same method signatures your delegate was expecting.Balinese
This solved the warnings for me. My interface was really half-baked while learning some Bluetooth.Mcfadden
B
3

What about session.delegate = (id)self. Maybe you just need to cast self as ID instead of const____strong.

EDIT: At the bequest of the OP, an explanation is in order. Type id is necessary for the protocol, because the protocol itself is literally typecast to id itself (id<GKSessionDelegate> etc.). My theory (because I am not using ARC in any of my projects) Is that the compiler gets very exacting so it can guarantee that your class is safe for release. You probably initialized your class in a non-id way... Of course I have no idea how, if anyone knows; I'd be happy to let them edit this answer.

EDIT 2: as Teddy said, adopting the protocols in your header file also silences this warning. I apologize for thinking it was implied that you had adopted the protocols.

Brewing answered 4/1, 2012 at 19:48 Comment(3)
That did clear the warnings. Thank you. Could you explain the ID versus const___strong?Dilation
I don't think this answer correctly fixes the warning...please see my answer.Balinese
Reddy, he's using ARC. In ARC, his class was probably instantiated as (nonatomic, strong). How would a typecast not fix that? And I think it was fairly obvious that he would have already conformed to the protocols.Brewing

© 2022 - 2024 — McMap. All rights reserved.