Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1027127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:11:33+00:00 2026-05-16T12:11:33+00:00

I’m having a problem with either typecasting or object scope. I’m getting an uncaught

  • 0

I’m having a problem with either typecasting or object scope. I’m getting an uncaught exception:

// Create the object here so that it's scope is outside the `if` statement, right?
searchTableViewController *newViewController;

if (rowSelected) {
    // Typecast the object to a searchTableViewController
    (searchTableViewController *)newViewController ;
    // Initialize and Allocate 
    newViewController = [[searchTableViewController alloc] initWithSearchBar:NO grouped:NO];
}else{
    // Typecast the global object to a personViewController
    (personViewController *)newViewController;
    // Initialize and Allocate
    newViewController = [[personViewController alloc] init];
}

// Act on the object: create a data model object and store it as a property, etc.
newViewController.myDataModel = [[dataModel alloc] initWithSelected:selectedField delegate:newViewController];

I have 2 similar ViewControllers searchTableViewController and personViewController. So I want to be able to instantiate either one or the other, using the same name, that way the rest of my code can act on the viewController using common properties and such.

This is causing a Terminating due to uncaught exception and regardless it seemed like the wrong way to do it anyway. I need help either in the typecasting department or I need help understanding how to declare the scope of these objects properly so I can work with them in and outside of the if statement.

Update


The easiest thing would be for me to write my code as follows. How can I declare, allocate and instantiate an object within an if statement and then have it accessible outside?:

if (rowSelected) {
    searchTableViewController *newViewController = [[searchTableViewController alloc] initWithSearchBar:NO grouped:NO];
}else{
    personViewController *newViewController = [[personViewController alloc] init];
}

// This will probably give an error since newViewController is not in the proper scope.
newViewController.myDataModel = [[dataModel alloc] initWithSelected:selectedField delegate:newViewController];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:11:34+00:00Added an answer on May 16, 2026 at 12:11 pm

    You want to declare your newViewController as the common subclass of the two objects you’re possibly allocating. Probably UIViewController.

    UIViewController *newViewController;
    
    if (rowSelected) {
        // Initialize and Allocate 
        newViewController = [[SearchTableViewController alloc] initWithSearchBar:NO grouped:NO];
    }else{
        // Initialize and Allocate
        newViewController = [[PersonViewController alloc] init];
    }
    

    And the cast operation isn’t doing anything when you use it in-place like that.


    Edit – if both of those classes have common properties, like dataModel, then you can avoid warnings by creating a common base class that derives from UIViewController and which contains those properties. You’d then change the declaration of your view controller variable in the first line above to match the intermediate base class.

    Later Edit — if you don’t want to create an intermediate base class, you can do the following (newViewController still must be declared as a UIViewController):

    if([newViewController respondsToSelector:@selector(setMyDataModel:)]) {
        DataModel *dataModel = [[dataModel alloc] initWithSelected:selectedField delegate:newViewController];
        [newViewController performSelector:@selector(setMyDataModel:) withObject:dataModel];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.