---
title: "Issue with NSString method initWithData: encoding: in Objective-C"  
description: "Issue with NSString method initWithData: encoding: in Objective-C"  
author: "Anonymous User"  
published: 2015-12-13  
updated: 2015-12-14  
canonical: https://www.mindstick.com/forum/33724/issue-with-nsstring-method-initwithdata-encoding-in-objective-c  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Issue with NSString method initWithData: encoding: in Objective-C

I have an issue with the initWithData: [method](https://www.mindstick.com/forum/166/webservice-method) of NSString, here is [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):

```
NSData *dataObj = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];NSString *strObj = [[NSString alloc] initWithData:dataObj encoding:NSUTF8StringEncoding];
```

Here, [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with initWithData: is that it does't return an [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of NSString, I tried doing an [explicit](https://www.mindstick.com/interview/2677/can-you-explain-about-implicit-and-explicit-type-casting) type-[cast](https://answers.mindstick.com/qa/97273/how-to-cast-a-baitcaster) using (NSString *) as well without much luck. This is [giving](https://yourviews.mindstick.com/view/80639/regifting-is-far-better-than-giving-away-a-bouquet) me [compiler](https://www.mindstick.com/articles/27/just-in-time-compiler) warnings when I try to pass the stringReply to a method I've written with type mismatches. Given I treat all warnings as [errors](https://answers.mindstick.com/qa/116170/fresh-fir-against-gandhis-in-national-herald-case-cover-up-for-ed-s-own-errors), I'd really like to understand what stringReply is being returned as and how I can enforce it to be of type NSString.

## Replies

### Reply by Tarun Kumar

I noticed that there is a type mismatch from the compiler. When you are trying to use **strObj** in another location has nothing to do with the object being returned from **initWithData:encoding:** and everything to do with where **stringReply** is subsequently being used.\
For example, if you are getting a type mismatch when you do this:

```
CustomClass *customObj;NSString *strObj;strObj = [[NSString alloc] initWithData:dataObj encoding:NSUTF8StringEncoding];[customObj methodWithString:strObj];
```

The problem is not that initWithData:encoding: is returning the wrong type but one of:\
**methodWithString:** doesn't actually take an NSString*\
**methodWithString:** is not properly declared/included before being used\
The second option will be happen if **CustomClass** is forward declared in a header file (as @class CustomClass) but never subsequently included in the implementation file.


---

Original Source: https://www.mindstick.com/forum/33724/issue-with-nsstring-method-initwithdata-encoding-in-objective-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
